frontend / state managers / redux / mapstatetoprops.md

mapStateToProps in Redux

3 interview angles 2 min read source

mapStateToProps in Redux

mapStateToProps is a function used with the connect higher-order component (HOC) from the react-redux library. It allows you to specify which parts of the Redux state your React component needs as props.

Table of Contents


What is mapStateToProps?

  • mapStateToProps is a function that takes the Redux store state and returns an object containing the data you want to pass as props to your component.
  • It is used with the connect function to connect React components to the Redux store.

How does it work?

  • When you use connect(mapStateToProps)(Component), the component will receive the returned object as props.
  • Whenever the Redux state changes, mapStateToProps is called again, and the component re-renders if the mapped props have changed.

Signature:

const mapStateToProps = (state, ownProps?) => ({
  // return an object with props
});
  • state: The entire Redux store state.
  • ownProps (optional): The props passed to the component from its parent.

Usage Example

import React from 'react';
import { connect } from 'react-redux';

const UserProfile = ({ user }) => (
  <div>
    <h2>{user.name}</h2>
    <p>Email: {user.email}</p>
  </div>
);

const mapStateToProps = (state, ownProps) => ({
  user: state.users.byId[ownProps.userId]
});

export default connect(mapStateToProps)(UserProfile);

Best Practices

  • Only select the data your component needs to avoid unnecessary re-renders.
  • Use selectors (e.g., with reselect) for derived or computed data.
  • Avoid returning new objects/arrays unless the data actually changed.
  • Use ownProps to access component props if needed.

Common Interview Questions

Q: What happens if mapStateToProps returns a new object every time?

  • The component will re-render on every state change, even if the data didn’t change. Use memoization or selectors to prevent this.

Q: Can you use hooks instead of mapStateToProps?

  • Yes, with React-Redux hooks (useSelector), you can access state directly in functional components.

Q: How do you optimize mapStateToProps for performance?

  • Use memoized selectors (e.g., reselect), and avoid unnecessary computations inside mapStateToProps.

Summary

  • mapStateToProps connects Redux state to component props.
  • Use it with connect for class components or legacy code.
  • Prefer hooks (useSelector) for new functional components.

Interview angle

  • “What is mapStateToProps for?” - selecting the slice of store state a component needs and passing it as props. connect re-runs it on every store change and shallow-compares the result to decide whether to re-render.
  • “Why does returning a new object cause extra renders?” - the shallow comparison is per top-level key, so { items: state.items.filter(...) } produces a new array every time and always fails the check. Memoize the derivation with a reselect selector.
  • “Is it still current?” - useSelector replaced it for function components. Same trap applies: useSelector compares the returned value with === by default, so returning a fresh object re-renders every time - pass shallowEqual or select primitives.