Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing props to react-redux container component

I have a react-redux container component that is created within a React Native Navigator component. I want to be able to pass the navigator as a prop to this container component so that after a button is pressed inside its presentational component, it can push an object onto the navigator stack.

I want to do this without needing to hand write all the boilerplate code that the react-redux container component gives me (and also not miss out on all the optimisations that react-redux would give me here too).

Example container component code:

const mapStateToProps = (state) => {     return {         prop1: state.prop1,         prop2: state.prop2     } }  const mapDispatchToProps = (dispatch) => {     return {         onSearchPressed: (e) => {             dispatch(submitSearch(navigator)) // This is where I want to use the injected navigator         }     } }  const SearchViewContainer = connect(     mapStateToProps,     mapDispatchToProps )(SearchView)  export default SearchViewContainer 

And I'd want to be able to call the component like this from within my navigator renderScene function:

<SearchViewContainer navigator={navigator}/> 

In the container code above, I'd need to be able to access this passed prop from within the mapDispatchToProps function.

I don't fancy storing the navigator on the redux state object and don't want to pass the prop down to the presentational component.

Is there a way I can pass in a prop to this container component? Alternatively, are there any alternative approaches that I'm overlooking?

Thanks.

like image 944
Mike Avatar asked Jun 12 '16 19:06

Mike


People also ask

How do I pass Props to another component?

As said, there is no way passing props from a child to a parent component. But you can always pass functions from parent to child components, whereas the child components make use of these functions and the functions may change the state in a parent component above.

Can we pass Props to functional component?

Passing properties from parent to child component using functional components. To access properties from parent to child using a functional component, users don't need to use 'this. props' like class components. Users can access props value by writing variable names only.

Can we pass component as props in React?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop. Copied!


2 Answers

mapStateToProps and mapDispatchToProps both take ownProps as the second argument.

[mapStateToProps(state, [ownProps]): stateProps] (Function): [mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function): 

For reference

like image 128
Abhinav Singi Avatar answered Sep 21 '22 10:09

Abhinav Singi


You can pass in a second argument to mapStateToProps(state, ownProps) which will give you access to the props passed into the component in mapStateToProps

like image 35
Conor Hastings Avatar answered Sep 19 '22 10:09

Conor Hastings