Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing props to React Router children routes

Tags:

I'm having trouble overcoming an issue with react router. The scenario is that i need to pass children routes a set of props from a state parent component and route.
what i would like to do is pass childRouteA its propsA, and pass childRouteB its propsB. However, the only way i can figure out how to do this is to pass RouteHandler both propsA and propsB which means every child route gets every child prop regardless of whether its relevant. this isnt a blocking issue at the moment, but i can see a time when i'd be using the two of the same component which means that keys on propA will overwritten by the keys by the keys of propB.

# routes routes = (   <Route name='filter' handler={ Parent } >     <Route name='price' handler={ Child1 } />     <Route name='time' handler={ Child2 } />   </Route> )  # Parent component render: ->   <div>     <RouteHandler {...@allProps()} />   </div>  timeProps: ->   foo: 'bar'  priceProps: ->   baz: 'qux'  # assign = require 'object-assign' allProps: ->   assign {}, timeProps(), priceProps() 

This actually works the way i expect it to. When i link to /filters/time i get the Child2 component rendered. when i go to /filters/price i get the Child1 component rendered. the issue is that by doing this process, Child1 and Child2 are both passed allProps() even though they only need price and time props, respectively. This can become an issue if those two components have an identical prop name and in general is just not a good practice to bloat components with unneeded props (as there are more than 2 children in my actual case).
so in summary, is there a way to pass the RouteHandler timeProps when i go to the time route (filters/time) and only pass priceProps to RouteHandler when i go to the price route (filters/price) and avoid passing all props to all children routes?

like image 959
PhilVarg Avatar asked Aug 06 '15 18:08

PhilVarg


People also ask

How do you pass props in react router route?

If you are using react-router v4, you can pass it using the render prop. Sometimes you need to render whether the path matches the location or not. In these cases, you can use the function children prop. It works exactly like render except that it gets called whether there is a match or not.

How do you pass router props to child component react?

But how would you pass props to the child component in React Router? You can use the render prop instead of the component prop for passing props to the child component. The route props include match, location, and history which are used to get the current route state from React Router within your component.


2 Answers

I ran into a similar issue and discovered that you can access props set on the Route through this.props.route in your route component. Knowing this, I organized my components like this:

index.js

React.render((   <Router history={new HashHistory()}>     <Route component={App}>         <Route           path="/hello"           name="hello"           component={views.HelloView}           fruits={['orange', 'banana', 'grape']}         />     </Route>   </Router> ), document.getElementById('app')); 

App.js

class App extends React.Component {   constructor(props) {     super(props);   }    render() {     return <div>{this.props.children}</div>;   } } 

HelloView.js

class HelloView extends React.Component {   constructor(props) {     super(props);   }    render() {     return <div>       <ul>         {this.props.route.fruits.map(fruit =>            <li key={fruit}>{fruit}</li>         )}       </ul>     </div>;   } } 

This is using react-router v1.0-beta3. Hope this helps!


Ok, now that I'm understanding your issue better, here's what you could try.

Since your child props are coming from a single parent, your parent component, not react-router, should be the one managing which child gets rendered so that you can control which props are passed.

You could try changing your route to use a param, then inspect that param in your parent component to render the appropriate child component.

Route

<Route name="filter" path="filter/:name" handler={Parent} /> 

Parent Component

render: function () {   if (this.props.params.name === 'price') {     return <Child1 {...this.getPriceProps()} />   } else if (this.props.params.name === 'time') {     return <Child2 {...this.getTimeProps()} />   } else {     // something else   } } 
like image 155
Jim Skerritt Avatar answered Oct 18 '22 08:10

Jim Skerritt


In child component, insted of

return <div>{this.props.children}</div> 

You may merge props with parent

var childrenWithProps = React.cloneElement(this.props.children, this.props); return <div>{childrenWithProps}</div> 
like image 26
Alex Shwarc Avatar answered Oct 18 '22 09:10

Alex Shwarc