Design comparison
Solution retrospective
Hello. In this project I chose to navigate to the details page using Redirect from react router. I had some problem when I wanted to redirect from one country details to another country details page. I had to use different path for that kind of redirects. Does anyone knows If there is a better way. I feel like this is not the most "elegant" solution. Thanks.
Community feedback
- @davidomarfPosted about 4 years ago
Hey!
Yes, there's a more elegant and idiomatic solution: Link.
It's a component that when clicked, will automatically redirect you to what you tell it to. Without weird handlers that render a
<Redirect>
.Then you move your styles from the
div.whateverClass
toa.whateverClass
.<Link to={`/countries/${country.name}`}> <div className={styles.flag}> <img src={country.flag} alt={country.name} /> </div> <div className={styles.info}> //... </div> </Link>
1@davidomarfPosted about 4 years ago@davidomarf And remove the
<Route path="/countries/redirect/:name component={DetailsRedirect}/>
.Handle all your redirects with
Link
.0@pogovPosted about 4 years ago@davidomarf Can you help with another thing? In the DetailsTemplate component I am maping over border countries and I've used Redirect here as well to navigate to the Details page of the border country. Now I wanted to refactor using Link but I have a problem because after user clicks on the link the url is changing as expected but component does not re-render with the new country details (it renders only when user manualy refresh the page). Thank you in advance.
0@davidomarfPosted about 4 years ago@pogov I think that is happening because it's rendering the same route. So it's rendering the same component. And you only handle the "DidMount" life cycle.
This means that all your logic is only running the first time you mount the component.
To fix it, you could handle those changes yourself using
componentDidUpdate
. If you try this, start loggingthis.props
. The logic should be similar to what you have incomponentDidMount
(you could even move all that into a separate function, and then just call that function in bothcomponentDidMount
andcomponentDidUpdate
.However, there an easier fix, and it's adding a key when you're rendering the page from the router. So it'd be like this:
<Route exact path="/countries/:name" render={(props) => ( <Details key={props.match.params.name} {...props} />) } />
With this, when the route changes, it'll match that path. And instead of just saying "Oh, yeah, I have already rendered that, here it is again", it'll create a new component
Details
that has thekey
:name
that matched that path.
If a key changes, it'll always re-render a component. You can read more about it here: https://kentcdodds.com/blog/understanding-reacts-key-prop
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord