How to use React Prompt with nested Routers? - javascript

Let's say I have 2 very simple Routers. Each of them is a separate component.
First one is the ParentRouter:
<TopComponent></TopComponent>
<Router>
<Switch>
<Route path="/child" component={ChildRouter}>
</Route>
<Route path="/outside" component={OutsideComponent}>
</Route>
</Switch>
</Router>
Second one is the ChildRouter:
<Router>
<Switch>
<Route exact path="/child" component={ListOfExampleComponents}>
</Route>
<Route path="/child/example" component={ExampleComponent}>
</Route>
</Switch>
</Router>
TopComponent is displayed all the time on every URL.
When I am on /child/example I want to have a React Prompt displayed if I click a Link to the /outside or to the /child (the Link can be placed inside ExampleComponent or TopComponent) or use back button to go back to the /child.
How to do it?

Ok, I found out what is the answer.
I added Prompt as it is intended in the Parent Router and deleted <Router> tags in the Child Router.

Related

Render multiple elements in React Router v6.+

I need the answer to this question: Render multiple components in React Router but for the newer version of react-router-dom (I'm using v6.0.2)
the older version of router-dom would work like this:
<Route path="/">
<Header/>
<Home/>
</Route>
while the new one looks like this:
<Route exact path="/" element={<Home/>}/>
I'm not sure how to add the Header as well
Try wrap them in a fragment
<Route exact path="/" element={<><Header/><Home/></>}/>
I ran in to the same issue, but also needed one page to get data from the parent element.
<Route path="/" element={<><Header/><Home children={<HomeDataProvider/>}/></>} />
try this if u want to add two or more component just put them inside of fragment tag
<Route exact path="/" element={<></>}/>
for Render multiple components in React Router for the newer version of react-router-dom (I'm using v6.5.0)
<Route path="/" element={
<>
< AddTodo addTodo={ addTodo }/>
<Todos todos={todos} onDelete={onDelete}/>
</>}>
</Route>

React Router DOM is rendering 2 components

This is my router structure:
<Router>
<main className="py-3">
<Switch>
<Container>
<Route exact path="/admin" component={AdminScreen}></Route>
<Route path="/:campaignId" component={CampaignScreen}></Route>
<Route path="/" component={HomeScreen} exact></Route>
</Container>
</Switch>
</main>
</Router>
For "some" reason, when I go to /admin i also get the CampaignScreen rendered.
I added the <Switch> as you can see, but it does not seem to help.
Where lies the problem?
Since admin can theoretically also be a campaignId react router thinks it matches both routes, and you have the <Container> element directly inside the Switch it still renders both, the solution is to have your routes directly in the Switch

Is it possible to use MemoryRouter nested in BrowserRouter in React?

I have been searching, and trying for a while now, however I couldn't find an answer if it is possible to use MemoryRouter for only specific routes while I use BrowserRouter in general. I wan t to navigate to a certain component but not change the url, tried it like so, but it changes the url but not rendering the component, the complete opposite what I wish.
<BrowserRouter>
<Switch>
<Route path="/login" component={Login} exact />
<Route path="/" component={MainPage} />
<MemoryRouter>
<Route
path='/somecomponent'
component={SomeComponent}
/>
</MemoryRouter>
</Switch>
</BrowserRouter>
Routes inside the MemoryRouter are relative to the MemoryRouter, not to your current location as displayed in the URL bar.
As far as it's concerned, the current route is "/", and it will only render components at <Route path="/">. If you were to add
<Route path="/">
<Redirect to="/somecomponent" />
</Route>
directly under MemoryRouter it should go to the path you're looking for, and render as intended.

Re-render header after user logs in or logs out in react

so the question may seem a little confusing so let me explain,
this is my index file
<Router>
<Header />
<Switch>
<Route exact path="/" component={HomePage} >
</Route>
<Route exact path="/Login/:step?" component={Login}>
</Route>
</Switch>
</Router>
and here is basically how i handle getting to homepage after user logs in
this.props.history.push("/")
so the problem here is when i do that the header component doesn't re-render and cause of that i can't call my api inside componentDidMount() inside of header to update user information.
Put Header in HomePage
and if you need put also in Login page.. but I don't think the login need the same header like in Homepage.
The main router component should be clean.
<Router>
<Switch>
<Route exact path="/" component={HomePage} >
<Header />
</Route>
<Route exact path="/Login/:step?" component={Login}>
<Header />
</Route>
</Switch>
</Router>
If your app is getting big for - update user information - think of using state management like Redux

using same component for different route path in react-router v4

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?
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}
/>
One solution is use inline function with component, that will render a new component each time, But this is not a good idea.
Like this:
<Route exact path="/add-client" component={props => <ManageClient {...props} />}></Route>
<Route exact path="/edit-client" component={props => <ManageClient {...props} />}></Route>
Better solution would be, use componentWillReceiveProps lifecycle method in ManageClient component. Idea is whenever we render same component for two routes and switching between them, then react will not unmount-mount component, it will basically update the component only. So if you are making any api call or require some data do all in this method on route change.
To check, use this code and see it will get called on route change.
componentWillReceiveProps(nextProps){
console.log('route chnaged')
}
Note: Put the condition and make the api call only when route changes.
<Route exact path={["/add-client", "/edit-client"]}>
<manageClient />
</Route>
Reference
Version 5.2.0
https://reacttraining.com/react-router/web/api/Route/path-string-string
My problem was we used an common path in-between, which causes dynamic path to not working
<Switch>
<Route key="Home" path="/home" component={Home} />
<Route key="PolicyPlan-create" path="/PolicyPlan/create" component={PolicyPlanCreatePage} />
{/* <Route key="PolicyPlan-list" path="/PolicyPlan" component={PolicyPlanListPage} /> */}
<Route key="PolicyPlan-list" path="/PolicyPlan/list" component={PolicyPlanListPage} />
<Route key="PolicyPlan-edit" path="/PolicyPlan/edit/:id" component={PolicyPlanCreatePage} />
<Route key="cardDesign" path="/cardDesign" component={cardDesign} />
<Route key="Admin-create" path="/admin/create" component={RegisterPage} />
</Switch>
So don't use the path like the commented one, now the code is working
.................
this.props.history.push("/PolicyPlan/edit/" + row.PolicyPlanId);
.............
You can simply provide an array of paths in a single route tag as follows -
<Route exact path={["/add-client", "/edit-client"]} component={manageClient}></Route>

Categories