I see that the mapStateToProps
and mapDispatchToProps
function which are passed to the connect
function in Redux take ownProps
as a second argument.
[mapStateToProps(state, [ownProps]): stateProps] (Function):
[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function):
What is the optional [ownprops]
argument for?
I am looking for an additional example to make things clear as there is already one in the Redux docs
goto-bus-stop's answer is good, but one thing to remember is that, according to the author of redux, Abramov/gaearon, using ownProps in those functions makes them slower because they must rebind the action creators when the props change.
See his comment in this link: https://github.com/reduxjs/redux-devtools/issues/250
ownProps refers to the props that were passed down by the parent.
So, for example:
Parent.jsx:
Child.jsx:
If the
ownProps
parameter is specified, react-redux will pass the props that were passed to the component into yourconnect
functions. So, if you use a connected component like this:The
ownProps
inside yourmapStateToProps
andmapDispatchToProps
functions will be an object:And you could use this object to decide what to return from those functions.
For example, on a blog post component:
You could return Redux action creators that do something to that specific post:
Now you would use this component like so: