I have a News
component that requires some props and I want to make it whenever I click on the <Link />
and the corresponding category prop Re-render. It updates the URL but does not re-render the component.
<Routes> <Route path="/" element={ <News country={this.country} apiKey={this.API_KEY} pageSize={this.pageSize} category="general" /> } /> <Route path="/business" element={ <News country={this.country} apiKey={this.API_KEY} pageSize={this.pageSize} category="business" /> } /> <Route path="/entertainment" element={ <News country={this.country} apiKey={this.API_KEY} pageSize={this.pageSize} category="business" /> } /> </Routes>
These are mine <NavLink />
<li className="nav-item"> <NavLink className="nav-link" aria-current="page" to="/"> Home </NavLink> </li> {this.props.categories.map((category) => { return ( <li key={category} className="nav-item"> <NavLink to={`/${category}`} className="nav-link"> {category[0].toUpperCase() + category.slice(1, category.length)} </NavLink> </li> ); })}
We can simply add a unique
key
in theelement
component.endcphpcn will be re-rendered each time with some different
props.
react-router
/react-router-dom
Optimizes rendering by keeping the same component instance installed, even if it renders on multiple routes. This is a performance optimization that saves you the process of uninstalling and reinstalling the same component just to pass different props values to it. In other words, the component remains mounted even if the route changes, and dependency props value updates should be handled in thecomponentDidUpdate
lifecycle method or theuseEffect
hook.Based on the routing and props being passed, this
TheNews
component does have some dependencies on thecategory
props, since that's the only prop I see that is different.News
component should probably have auseEffect
hook and rely on thiscategory
property to run/load any different data based on this different property value.Example:
If
News
is a component based on the React class, then it should implement thecomponentDidUpdate
method.Additionally, based on this, since the
category
and URL paths also seem to match, in most cases you can also make your code drier by rendering a single route using :Category as route path parameter and apply the sameuseEffect
hook logic to re-run logic that depends on the category value.Example:
Similarly, if
News
is a class component, use the appropriatecomponentDidUpdate
lifecycle methods and implement customwithRouter
higher-order components, so thatcategory
route path parameters are injected as props.Using React keys on the
News
component should only be used as a last resort as it involves actual disassembly, such as unmounting and remounting the component, This requires more work than simply re-rendering the component with updated prop values.