Prefixing all react links within the app - reactjs

I have several links throughout my react app that look like this:
<Route path="/linkpath" component={Whatever} />
or
<Link to="/linkpath">Click Me</Link>
Is there a quick/easy way to prefix all the links throughout the app with a predetermined string? ... perhaps via a Router configuration param?
So for example if my prefix is "/foo", all links to "/linkpath" would be converted to /foo/linkpath.
Note: Just to clarify, I'm using react-router-dom v4.0.0-beta.5

It turns out the Router in react-router-dom v4.0.0-beta.5 has a param called basename that addresses this scenario.
<Router basename="/foo">
<Link to="/linkpath">Click Me</Link>
<!-- other links here -->
</Router>
In this case, Click Me's link will be /foo/linkpath. So, as long as Router wraps all Components in the app, all links will be prefixed with basename.
One important note: Make sure the string in basename starts with a slash; i.e., use "/foo" instead of "foo". The latter one produces undesired behavior when clicking links (it appends the full link path to the existing URL everytime you click the link, instead of replacing it).

Using React Router, if you defined your routes using nesting like this:
<Router history={history}>
<Route path="/" component={App}>
<Route path="/hello" component={Hello} />
<Route path="/whatever" component={Whatever} />
</Route>
</Router>
Then you can simply change the path of the outermost Route component and it will add the prefix. In the case above, you could change it to:
<Router history={history}>
<Route path="/api" component={App}>
<Route path="/hello" component={Hello} />
<Route path="/whatever" component={Whatever} />
</Route>
</Router>
And then all the nested routes will be at /api/hello, /api/whatever instead of the original /hello and /whatever.

Related

why two routes calling same page in react js that has different path link

<Route path={${process.env.PUBLIC_URL}/book-repair/:name/:name} component={Modals}/>
<Route path={${process.env.PUBLIC_URL}/book-repair/:name} component={Companies}/>
when call second route this is calling very well. but when am call first route first route and second route both are calling. please solve my issue.
Use Switch to render the Routes Exclusively and use exact to strictly match the pattern
import { Route, Switch } from "react-router";
let routes = (
<Switch>
<Route path={${process.env.PUBLIC_URL}/book-repair/:name/:name} component={Modals} exact />
<Route path={${process.env.PUBLIC_URL}/book-repair/:name} component={Companies} exact />
</Switch>
);
Refer: https://reactrouter.com/web/api/Switch
<Switch><Route path={${process.env.PUBLIC_URL}/book-repair/:name/:name} exact component={Modals}/>
<Route path={${process.env.PUBLIC_URL}/book-repair/:name} exact component={Companies}/> </Switch>
You should add exact and Switch to your routes.
If you are rendering the routes into a Router they both can be matched and rendered. The Router inclusivley matches and renders routes. Use the exact prop to exactly match one or the other. The issue is that ${process.env.PUBLIC_URL}/book-repair/:name is a prefix for ${process.env.PUBLIC_URL}/book-repair/:name/:name, and so will also be matched.
<Route
exact
path={`${process.env.PUBLIC_URL}/book-repair/:name/:name`}
component={Modals}
/>
<Route
exact
path={`${process.env.PUBLIC_URL}/book-repair/:name`}
component={Companies}
/>
The alternative is to render the routes into a Switch to exclusively match and render routes. Here path order and specificity matter. Order your routes from most specific to least specific. You don't need to specify the exact prop since the Switch only matches and renders the first matching Route.
<Switch>
...
<Route
path={`${process.env.PUBLIC_URL}/book-repair/:name/:name`}
component={Modals}
/>
<Route
path={`${process.env.PUBLIC_URL}/book-repair/:name`}
component={Companies}
/>
...
</Switch>

In ReactJs using routes, how do I pass a hidden parameter without being in the URL?

Currently in my code I need to pass a value from one component to the other through a button using router link ..
I am able to do this, but this value is appearing in the URL, appearing as if I had sent a GET.
I want to be able to pass this value on to the other component in a hidden way.
See the important parts of my code:
In TotalCartComponent:
<Link to={"/orderproduct/" + total}>ADVANCE</Link>
In OrderProduct:
<h1>HELLO!!</h1>
<h4>Total is: {this.props.match.params.total}</h4>
In AppShell:
<main>
<Switch>
<Route exact path="/" component={Main}/>
<Route path="/mycart" component={MyCart}/>
<Route path="/orderproduct/:total" component={OrderProduct}/>
<Route path="/view1" component={View1}/>
</Switch>
</main>

having issue with Route & Link in react-router-dom

I am working on a React project in which I use the Switch, Link and Route from react-router-dom to route to the paths. but now I'm facing an issue and in console I'm getting errors The context router is marked as required in Link, but its value is undefined and cannot read property history of undefined which is pointing to Link.js line number 76 . When i checked Link.js, there is a line where this.context.router.history is used, and seems like this.context.router is undefined. Couldn't figure out whats the real issue as this was working till yesterday. The version of react-router-dom I'm using is 4.1.1.
Can you provide some code example?
It seems like you are putting outside of BrowserRouter's scope (or HashRouter's).
must always be inside the scope of .
You can do this:
<BrowserRouter>
<Link ... />
<Layout>
<Switch>
<Route ... />
<Route ... />
<Route component={NotFound} />
</Switch>
</Layout>
</BrowserRouter>`
But you cannot do something like this:
<Link ... />
<BrowserRouter>
<Layout>
<Switch>
<Route ... />
<Route ... />
<Route component={NotFound} />
</Switch>
</Layout>
</BrowserRouter>`
User Kishan Jaiswal is pointing you in the right direction by the way, he's linking to an issue which wasn't a bug but only a misplaced Link component outside of react-router-dom Router.
just take a reference of this and match your route file
https://github.com/ReactTraining/react-router/issues/4759

React Router v4 rendering component twice

Here is my Router Implementation
<BrowserRouter>
<div>
<Route exact path="/" component={ProfilesIndex} />
<Route exact path="/profiles" component={ProfilesIndex} />
<Route exact path="/profiles/new" component={ProfileEditor} />
<Route exact path="/profiles/:id" component={ProfileEditor} />
</div>
</BrowserRouter>
When I am browsing to /profiles/new path, it is rendering the ProfileEditor component twice. For every other route, it is working fine.
Can someone suggest how to fix this problem ?
I found the answer after browsing into multiple sections in Router Docs. Problem was it was matching multiple routes
When user opens /profiles/new it matches two routes
/routes/new
/routes/:id
Because :id is like a wildcard * and it also matches new word so both routes get matched and hence the component gets rendered twice.
The solution is to wrap the routes with Switch if you do not want to match multiple routes.
<BrowserRouter>
<Switch>
<Route exact path="/" component={ProfilesIndex} />
<Route exact path="/profiles" component={ProfilesIndex} />
<Route exact path="/profiles/new" component={ProfileEditor} />
<Route exact path="/profiles/:id" component={ProfileEditor} />
</Switch>
</BrowserRouter>
For anyone coming here from v6+
exact prop no longer exists, the paths are exact by default if they aren't appended a wildcard *
However I was still getting a double render. I ran a prod build and checked and the double render is gone so probably nothing to worry about - sometimes hooks run twice in development mode (I guess that's what's happening internally)
for me, this is because of React.StrictNode which is wrapped arround App component.
it intentionally double render components (only in development) to enforce you, not use side effects on some of your component's
lifecycle events.
the reason behind that is documented here

React-router nested rotues

This is how my Routing looks:
<Router history={createBrowserHistory()}>
<Route path="/" component={App}>
<IndexRoute component={EventAppContainer}/>
<Route path="fights" component={FightApp}/>
<Route path="fighters" component={FightersAppContainer}>
<Route path="/fighter/:id" component={FighterDetails} />
</Route>
</Route>
</Router>
In FightersAppContainer I fetch some data, if I render everything from there my nested route works perfectly. But If I want to have more children components, for example FightersAppContainer=>FightersApp=>FighterItem then I cannot reach my nested route. I do not get any error it just doesn't show up. How can I fix this?
To be more clear if I use <Link to={"/fighter/"+fighter.id}>{fighter.last_name}</Link> inside FightersAppContainer I achieve what I want. But if I try that inside for example FighterItem component (that is a child of FightersAppContainer component) then I don't get the desired result, as a matter of fact I don't get anything including errors...
The problem is that this:
<Route path="/fighter/:id" component={FighterDetails} />
being a child of the fighters path, would be expecting the route to be /fighters/fighter/:id, so your Link element would have to look like this if you're not on the fighters element itself:
<Link to={"/fighters/fighter/"+fighter.id}>{fighter.last_name}</Link>

Resources