I have been all around internet about the dynamic routing of React. But I couldn't find anything which explains how it works and how it is different than static routing in every single sense.
I understood it pretty well how the things go when we want to render something in the same page using React-Route.
My question is how does it work when a whole new page is wanted to be rendered? Because in this case all the DOM inside that page has to be re-rendered. Thus would it be static routing? or still dynamic in some ways?
I hope I've been clear.
Thanks for the answers in advance, I appreciate!
I don't think the above explanation is correct for Static vs Dynamic routing.Also there is not much explanation in the web for it, but there is a very nice explanation in React Router Docs.From the Docs
If you’ve used Rails, Express, Ember, Angular etc. you’ve used static
routing. In these frameworks, you declare your routes as part of your
app’s initialization before any rendering takes place. React Router
pre-v4 was also static (mostly). Let’s take a look at how to configure
routes in express:
In Static routing, the routes are declared and it imported in the Top level before rendering.
Whereas in Dynamic routing
When we say dynamic routing, we mean routing that takes place as your
app is rendering, not in a configuration or convention outside of a
running app.
So in Dynamic routing, the routing takes place as the App is rendering.
The examples explained in the above answer are both for static routing.
For Dynamic routing it is more like
const App = () => (
<BrowserRouter>
{/* here's a div */}
<div>
{/* here's a Route */}
<Route path="/tacos" component={Tacos}/>
</div>
</BrowserRouter>
)
// when the url matches `/tacos` this component renders
const Tacos = ({ match }) => (
// here's a nested div
<div>
{/* here's a nested Route,
match.url helps us make a relative path */}
<Route
path={match.url + '/carnitas'}
component={Carnitas}
/>
</div>
)
First in App component only one route is declared /tacos.When the user navigates to /tacos the Tacos component is mounted and there the next route is defined /carnitas.So when the user navigates to /tacos/carnitas, the Carnitas component is mounted and so on.
So here the routes are initialized dynamically.
Use react-router and react-router-dom, and write something like this:
onSubmit((e) => {
e.preventDefault();
this.props.history.push('<url>')
}
so at any place you may run this line and go to another locations conditionaly
Related
I have a rooms list that I iterate, rendering the different rooms like this:
<Room room={room} key={room.id}/>
I want each room to redirect to their corresponding path (/rooms/:id). The only way of redirecting elements is via react-router-dom but I feel there must be a better way of achieving redirection in this case.
React router works fine, only thing you need is pass id to link
<Route path="/RoomsList/:roomId" element={<RoomCard/>}/>
in RoomCard you use hook
const {roomId} = useParams();
and change the component depending on the id.
I have a popup component overlaying the main content that I want to render through routing, but because I want it to build on existing routes without messing up my main page configuration, I have tried going for a nested route. However, I seem to have misunderstood how it is supposed to be done. I wrapped the export with "withRouter" and tried to do this:
<Fragment>
<Helmet>
<title>Videos - Saddex Productions</title>
</Helmet>
<Switch>
<Route path="/popup"
render={() => <Popup items={props.videos}/>}
/>
</Switch>
...
This doesn't work and only renders the main component. And I also don't want to outsource the code that's going to be rendered underneath, because it seems unneccessary. However, what should I do? Thanks in advance.
Solved: The reason it didn't work is because I presumed the router works like the Express router does - with relative paths. I included the base path like "videos/popup" and now it works, it seems.
I have a React Single Page App that has a static top bar in which I do not want to put any navigation. I want to change the view/page from a link that is inside the main view itself. I've had no luck so far.
Basically due to the fact that I wrote this app while learning React, means that I now realize my whole state and all functions are basically in the wrong place, they're in one page, and now that I am upgrading my Single Page App to have more views, I will have to share state between pages and thus lift it up to a new App that is the parent to both. All functions, logic and code is too ingrained in this one page so I'll have to improvise.
In the structure, the Home.jsx page contains all the app's State, and honestly I can not fathom even rewriting that right now. This is my first project and I really just need a win or I'll burn out. It's all very entrenched. So basically I want the Home.jsx page to remain the main page with all the State in it, because it works very well this way, and just pass functions and other state as props to Overview.jsx so it knows what's up.
App View
| |
| |
Home.jsx Overview.jsx
(main page) (second page)
| |
| |
This page should This one should have a
link
have a NavLink or that goes back to
something else Home.jsx
which triggers the App view
to load Overview.jsx
---and this is what I can't
seem to do.
I've tried React Router, but nowhere in the documentation or even online have I found an answer to how to link from one view/page to another from WITHIN the first page, and then to go back by clicking something WITHIN the now open second view.
you can use link or a tag to navigate from one to another from view. Examples
<link to='path' />
<a href='path'
You'll need to use <Router> to tell React what to render based on the path and then you can use <Link> in the different components. See React Router's basic routing example for more details.
Inside App.js:
<Router>
<Route path="/" exact component={Home} />
<Route path="/overview" component={Overview}
</Router>
Inside Overview.js:
<Link to="/">Home</Link>
I have a massive preexisting website into which some new pages are going to be added that are being built on (client-side) ReactJS. The routes at which the new ReactJS pages will live are deep in the site: The ReactJS pages have to play nice with the existing URL structure, and that means that in a lot of cases, they won't know what their base URL is until they're deployed to production. (In some cases, they still won't know, because the upper parts of the URLs, the parts before the ReactJS page, are parameterized and will be changing on the fly.)
However, React Router seems to like absolute paths, and doesn't seem to play well with partial/relative/unknown paths. I've solved this by querying document.location.pathname upfront when everything loads — but it feels clunky, like React Router should be able to handle these kinds of relative paths out-of-the-box.
So here's my code, which does work:
const baseRoute = document.location.pathname;
...
render() {
return <Router>
<Route path={baseRoute} component={App}>
<IndexRoute component={HomePage}/>
<Route path={baseRoute + "/sub-page1"} component={SubPage1}/>
<Route path={baseRoute + "/sub-page2"} component={SubPage2}/>
<Route path={baseRoute + "/*"} component={NotFoundPage}/>
</Route>
</Router>;
}
...
render() {
return <div>
<IndexLink to={baseRoute}>Home</IndexLink>
| <Link to={baseRoute + "/sub-page1"}>Sub-Page 1</IndexLink>
| <Link to={baseRoute + "/sub-page2"}>Sub-Page 2</IndexLink>
</div>;
}
But this seems like a very clunky, brute-force way to handle it. It means that anything that needs a path needs to have access to that baseRoute variable, which is computed once at startup. It means that everything that uses routing needs to remember to use the baseRoute variable as well. And, to make matters worse, the code can't really be as simple as what I have above, since the incoming URL could contain a child route that only React Router knows about, so in some cases, string massaging is needed to properly compute the baseRoute.
It looks like they may have solved this issue more generally in React Router 4 by adding a basename property, but we're on React Router 3 right now.
So is there a better solution in React Router 3 to the app being hosted at an unknown URL, or are we stuck with passing around a derived baseRoute property?
So I have a localised site, and need to redirect routes in certain cases. For example, if a user had their UI set to Spanish, and went to mysite.com/about, they would need to be redirected to mysite.com/es/about.
So basically, the routes are duplicated via <Route> components from react-router:
{Object.values(UILanguages).map(locale => {
return (
<Route path={`${locale.basepath}`} key={locale.basepath} locale={locale} component={App} status={200}>
...routes go here
</Route>
)
})}
It would be simple enough to check within the componentWillMount lifecycle method of <App> to find out if I'm on the wrong <Route> component, but then I would have to prepend every single link in my application, which I don't want to do. Instead, I would like dynamic redirecting for foreign languages, as displayed in the first paragraph.
How is this possible?
Use the react router hooks instead it will be cleaner (smaller components) and simpler than using the component lifecycle.
https://github.com/ReactTraining/react-router/blob/master/docs/API.md#onenternextstate-replace-callback
in your case you need to use all 3 arguments because the first one is the next state the second is the redirection you want to make and the third is a callback to call when you are done testing values.
i can give you an exemple about how to make this but i don't really know how your app is structured so instead check this link , it helped me a lot:
https://www.youtube.com/watch?v=JicUNpwLzLY&t=359s
hope this helps!
ps: in some cases you need to define the onChange hook too example : pagination...