Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  142] [ 4]  / answers: 1 / hits: 24261  / 6 Years ago, tue, february 27, 2018, 12:00:00

I am trying to have separate routes but same component for add/edit forms in my react app like the below:



<Switch>
<Route exact path=/dashboard component={Dashboard}></Route>
<Route exact path=/clients component={Clients}></Route>
<Route exact path=/add-client component={manageClient}></Route>
<Route exact path=/edit-client component={manageClient}></Route>
<Route component={ NotFound } />
</Switch>


Now in the manageClient component, I parse the query params (I pass in a query string with client id in edit route), I render conditionally based on the query param passed.



The problem is that this doesn't remount the whole component again. Say an edit page is opened, and the user clicks on add component, the URL changes, but the component doesn't reload and hence remains on the edit page.



Is there a way to handle this?


More From » reactjs

 Answers
41

Using different key for each route should force components to rebuild:



    <Route 
key=add-client
exact path=/add-client
component={manageClient}
/>

<Route
key=edit-client
exact path=/edit-client
component={manageClient}
/>

[#55056] Thursday, February 22, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylerdamiena

Total Points: 139
Total Questions: 90
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
tylerdamiena questions
;