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?
mapStateToPropsis 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
connectfunction 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,
mapStateToPropsis 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
ownPropsto 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 insidemapStateToProps.
Summary
mapStateToPropsconnects Redux state to component props.- Use it with
connectfor class components or legacy code. - Prefer hooks (
useSelector) for new functional components.
Interview angle
- “What is
mapStateToPropsfor?” - selecting the slice of store state a component needs and passing it as props.connectre-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?” -
useSelectorreplaced it for function components. Same trap applies:useSelectorcompares the returned value with===by default, so returning a fresh object re-renders every time - passshallowEqualor select primitives.