Subscribe to selected state
A presentational component needs selected Redux values as props and should update when those values change.
Redux Toolkit · Function
connect() wraps a React component with access to selected Redux state and dispatchable actions. It remains supported, but the documentation recommends the hooks API for new code.
connect() returns a new wrapper component around the supplied React component. The wrapper can subscribe to store updates through mapStateToProps, inject dispatchable actions through mapDispatchToProps, and combine those values with the wrapped component's props.
connect(mapStateToProps?, mapDispatchToProps?, mergeProps?, options?)(Component)| Name | Type | Description |
|---|---|---|
mapStateToProps | Function | null | undefined | Optional function selecting component data from the Redux state; its result must be a plain object. |
mapDispatchToProps | Function | Object | Optional function or object that supplies dispatchable actions or dispatch to the wrapped component. |
mergeProps | Function | Optional function that controls how state props, dispatch props, and own props are combined. |
options | Object | Optional connection configuration object. |
A function that accepts a React component and returns a new connected wrapper component.
import { connect } from "react-redux";
import { increment } from "./counterSlice";
function Counter({ value, increment }) {
return <button onClick={increment}>{value}</button>;
}
const mapStateToProps = (state) => ({ value: state.counter.value });
export default connect(mapStateToProps, { increment })(Counter);The wrapper reads the selected state and injects the action creator as a prop; clicking the button dispatches the action.
A presentational component needs selected Redux values as props and should update when those values change.
A component should invoke Redux action creators without manually calling dispatch.
An existing class or function component already uses the classic React-Redux connection pattern.
connect() is a higher-order component function: it accepts optional mapping functions or objects and returns a function that wraps a component. The result is a new connected component, while the original component is left unchanged.
Follow-up: What happens when mapStateToProps is provided? The wrapper subscribes to store updates and recalculates the mapped props whenever the store changes.
mapStateToProps receives the complete Redux state and should return a plain object containing the values needed by the wrapped component. The returned fields are merged into that component's props.
Follow-up: How can subscription be disabled? Pass null or undefined for mapStateToProps.
mapDispatchToProps can be an object, a function, or omitted. With an object, action creators are bound so calls from the component dispatch their actions; when it is omitted, dispatch is supplied by default.
Follow-up: Which approach is preferred for new React code? The documentation recommends the hooks API, which is simpler and works better with TypeScript.
mapStateToPropsmapDispatchToPropsmergePropsProvideruseSelectoruseDispatch