简体   繁体   中英

Prevent re-render with every URI for React Router params

Given the following route, if a URI query is added or changed (ie ?bar2=foo ) then MySpecialComponent will rerender even though it isn't set to capture these params:

<Route exact path="/foo/:bar" render={props => (<MySpecialComponent/>)}/>

This causes a massive amount of re-rendering when used throughout. How can the unwanted params be ignored? Ie to not triggering a re-render until the bit we care about changes ( /foo/:bar ).

I found using render on route along with passing the params myself to be the cleanest solution (to not pollute containers with router logic):

<Route exact path="/foo/:bar" render={props => (
  // Only pass needed props to avoid unnecessary re-render on ANY URI change
  <MySpecialComponent bar={props.match.params.bar}/>
)}/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM