Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ownProps in react-redux?

Tags:

I am reading the API on react-redux and looking at one of Redux' github examples: Redux todo app

One of the containers, FilterLink, has mapDispatchToProps (and mapStateToProps) to accept two arguments, one of them being ownprops.

const mapDispatchToProps = (dispatch, ownProps) => ({   onClick: () => {     dispatch(setVisibilityFilter(ownProps.filter))   } }) 

The API docs says:

"If your mapStateToProps function is declared as taking two parameters, it will be called with the store state as the first parameter and the props passed to the connected component as the second parameter, and will also be re-invoked whenever the connected component receives new props as determined by shallow equality comparisons. (The second parameter is normally referred to as ownProps by convention.)"

I still can't fully grasp what it does. Can someone explain what ownProps does with a different example?

like image 603
Iggy Avatar asked Dec 05 '17 06:12

Iggy


People also ask

What is ownProps in mapStateToProps?

The mapStateToProps(state, ownProps) is specified as the first argument of connect() call and its ownProps parameter receives the props object of the wrapper component. If the ownProps parameter is not present, React Redux skips calling the function at the props change.

What is the use of Connect in Redux?

The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store.

What is mapStateToProps and mapDispatchToProps?

mapStateToProps is a function that you would use to provide the store data to your component, whereas mapDispatchToProps is something that you will use to provide the action creators as props to your component.

What is useSelector and useDispatch?

useSelector and useDispatch are a set of hooks to use as alternatives to the existing connect() higher-order component. The equivalent of map state to props is useSelector. It takes in a function argument that returns the part of the state that you want. The equivalent of map dispatch to props is useDispatch.


1 Answers

ownProps are the attributes that are passed when the component is used. In plain React these would be just called props.

for example in Footer.js the FilterLink is used as:

<FilterLink filter="SHOW_ALL">   All </FilterLink> 

So mapStateToProps method would be called with ownProps having the value:

{   "filter": "SHOW_ALL",   "children": ... } 

The mapStateToProps method is used in a Redux-wrapped component to combine the explicitly passed properties (ownProps) with the state handled by the Redux store into the props of the wrapped component.

So in your linked example of FilterLink

const mapStateToProps = (state, ownProps) => ({   active: ownProps.filter === state.visibilityFilter }) 

the component is active (this.props.active == true) if the filter attribute (e.g. SHOW_ALL) matches the visibiltyFilter in state, i.e. if it is currently filtered by this value.

like image 53
Harald Gliebe Avatar answered Oct 31 '22 00:10

Harald Gliebe