Using React Suspense with react-router-dom - javascript

I am trying to implement lazy loading to my projects so I can lazy load routes of react-router-dom. While going through internet I noticed that there are two ways to implement it - wrapping all routes with one React.Suspense or putting every page withing its own React.Suspense. The thing is that I would like to know if there are any differences between these two methods and if there are, what are their's advantages and disadvantages?
Routes wrapped in one Suspense
<React.Suspense fallback={<p>Loading...</p>}>
<Routes>
<Route path="/" element={<MainPage/>}></Route>
<Route path="/todo/:todoID" element={<TodoPage/>}></Route>
<Route
path="/user/:user"
element={<UserTodos/>}
></Route>
</Routes>
</React.Suspense>
Every route with its own suspense
<Routes>
<Route
path="/"
element={
<React.Suspense fallback={<p>Loading...</p>}>
<MainPage />
</React.Suspense>
}
></Route>
<Route
path="/todo/:todoID"
element={
<React.Suspense fallback={<p>Loading...</p>}>
<TodoPage />
</React.Suspense>
}
></Route>
<Route
path="/user/:user"
element={
<React.Suspense fallback={<p>Loading...</p>}>
<UserTodos />
</React.Suspense>
}
></Route>
</Routes>

Related

react-router-dom passing useState for reusing other routes

So I can't see something react-hooks related here and I don't get the logic a little bit in react component with those passing state in routes.
So basically something like this.
<Router>
<Navbar></Navbar>
<Routes>
<Route path="/guest" element={<Guest></Guest>}></Route>
<Route path="/saved" element={<SavedItem></SavedItem>}></Route>
<Route path="/selectedgallery" element={<SelectedGallery></SelectedGallery>}></Route>
<Route path="/selectedsavedgallery" element={<SelectedSaveGallery></SelectedSaveGallery>}></Route>
<Route path="/login" element={<Login></Login>}></Route>
<Route path="/register" element={<Register></Register>}></Route>
<Route path="/" element={<Gallery></Gallery>}></Route>
<Route path="/create" element={<CreatePin></CreatePin>}></Route>
</Routes>
</Router>
As you see I have <Login> and what I want to do I will pass my useState() in that area and when I get the user I will pass it in my other routes so I could reuse it for users that have login.
I imagine it somehow look like this
const [owner,setOwner] = useState()
...
<Route path="/login" element={<Login></Login>} {whatever here to pass the "setOwner" and "owner".} ></Route>
...
Yes,so I'm passing the props in any routes so that I could reuse it and then other routes can used it for their own purposes.
setOwner can be passed to the Login element as an parameter
<Route path="/login" element={<Login setOwner={setOwner}/>} />
in Login
function Login({setOwner}) {
...
setOwner("foo");
...
}

React Router V6 routed component needs state/props from another routed component

Hello I am using React router for my application. App.js code segment:
<Routes>
<Route path="/" element={<Login navigate={navigate} setOverlays={setOverlays} setToken={setToken} setUser={setUser} />} />
<Route path="/create-account" element={<CreateAccount />} />
<Route path="/projects" element={<Projects token={token} name={user.name} jobTitle={user.jobTitle} navigate={navigate} setOverlays={setOverlays} />} />
<Route path="/individual_project" element={<IndividualProject />} />
</Routes>
The last route (/individual_project) requires props that is located in the projects component. Since I do not have access to the components in App.js how can I create this route.
I also prefer not to move the state variables from the Projects component and pass them down through props.
There are few ways to achieve that:
You can move the individual_project route inside the Projects component like this:
const Projects = () => {
...other logic
return(
<Routes>
<Route path="/individual_project" element={<IndividualProject />} />
</Routes>
)
}
Docs here: https://reactrouter.com/docs/en/v6/getting-started/overview#nested-routes
Alternatively, you could also create a context to wrap the routes. Then, you can retrieve the variables directly from the context:
<Context.Provider value={yourValuesToShare}>
<Routes>
<Route path="/" element={<Login navigate={navigate} setOverlays={setOverlays} setToken={setToken} setUser={setUser} />} />
<Route path="/create-account" element={<CreateAccount />} />
<Route path="/projects" element={<Projects token={token} name={user.name} jobTitle={user.jobTitle} navigate={navigate} setOverlays={setOverlays} />} />
<Route path="/individual_project" element={<IndividualProject />} />
</Routes>
</Context.Provider>
Docs here: https://reactjs.org/docs/context.html
For this kind of scenarios , using Context API is recommended .its more precise and cleaner that the other techniques.
React docs
Context api is used to access state from any component so react context api is recommended

React router order of routes, does it matter?

I set up my react router like this:
<Route exact path="/users">
<component/>
</Route>
<Route exact path="/users/:id">
<component/>
</Route>
And it wasn't working correctly (url was changing but content wasn't)
after I changed the order of Route in my Routes.js (the one with :id first) it started working. Do it matters or there is other problem somewhere in my code?
Yes you need to check
Route and
Switch
<Switch> is unique in that it renders a route exclusively. In contrast, every <Route> that matches the location renders inclusively.
Read and explore more at https://reactrouter.com/web/api/Switch
Yes, nested routes should be above its parent.
So in the following code, about/user has to be above /about. If its beneath then only /about will render
<Route path='about/user' />
<Router path='about/' />
Basic React Router Setup
<Router>
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/users">
<Users />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</Router>

React routing - NotFound component does not load in all cases

I am a beginner in reactJS. I have the following routes defined in App.js
<Router>
<Switch>
<Route exact path='/' component={Login} />
<Route path='/dashboard' component={Dashboard} />
<Route path="*" component={NotFound} ></Route>
</Switch>
</Router>
The NotFound component should be displayed for any invalid route. It works perfectly for all invalid route that looks like this
localhost:3000/something_invalid
localhost:3000/something_invalid_1
but if I add one more step to the URL. The NotFound component is not getting rendered. e.g.,
localhost:3000/something_invalid/something_invalid
localhost:3000/something_invalid_1/something_invalid_1
What could I be doing wrong ?
<Router>
<Switch>
<Route exact path='/' component={Login} />
<Route path='/dashboard' component={Dashboard} />
<Route component={NotFound} ></Route>
</Switch>
</Router>
Code updated.
A 'Switch' renders the first child 'Route' that matches. A 'Route' without a path always matches.
The asterisk in your example is used in React-Router until version 3.
If you use the root URL, it'll work as a catch all
Note that this works because it comes after your other defined routes, ie. /dashboard still works
<Router>
<Switch>
<Route exact path='/' component={Login} />
<Route path='/dashboard' component={Dashboard} />
<Route path="/" component={NotFound} ></Route>
</Switch>
</Router>

React-Router: No Not Found Route?

Consider the following:
var AppRoutes = [
<Route handler={App} someProp="defaultProp">
<Route path="/" handler={Page} />
</Route>,
<Route handler={App} someProp="defaultProp">
<Route path="/" handler={Header} >
<Route path="/withheader" handler={Page} />
</Route>
</Route>,
<Route handler={App} someProp="defaultProp">
<Route path=":area" handler={Area} />
<Route path=":area/:city" handler={Area} />
<Route path=":area/:city/:locale" handler={Area} />
<Route path=":area/:city/:locale/:type" handler={Area} />
</Route>
];
I have an App Template, a HeaderTemplate, and Parameterized set of routes with the same handler (within App template). I want to be able to serve 404 routes when something is not found. For example, /CA/SanFrancisco should be found and handled by Area, whereas /SanFranciscoz should 404.
Here's how I quickly test the routes.
['', '/', '/withheader', '/SanFranciscoz', '/ca', '/CA', '/CA/SanFrancisco', '/CA/SanFrancisco/LowerHaight', '/CA/SanFrancisco/LowerHaight/condo'].forEach(function(path){
Router.run(AppRoutes, path, function(Handler, state){
var output = React.renderToString(<Handler/>);
console.log(output, '\n');
});
});
The problem is /SanFranciscoz is always being handled by the Area page, but I want it to 404. Also, if I add a NotFoundRoute to the first route configuration, all the Area pages 404.
<Route handler={App} someProp="defaultProp">
<Route path="/" handler={Page} />
<NotFoundRoute handler={NotFound} />
</Route>,
What am I doing wrong?
Here's a gist that can be downloaded and experimented on.
https://gist.github.com/adjavaherian/aa48e78279acddc25315
DefaultRoute and NotFoundRoute were removed in react-router 1.0.0.
I'd like to emphasize that the default route with the asterisk has to be last in the current hierarchy level to work. Otherwise it will override all other routes that appear after it in the tree because it's first and matches every path.
For react-router 1, 2 and 3
If you want to display a 404 and keep the path (Same functionality as NotFoundRoute)
<Route path='*' exact={true} component={My404Component} />
If you want to display a 404 page but change the url (Same functionality as DefaultRoute)
<Route path='/404' component={My404Component} />
<Redirect from='*' to='/404' />
Example with multiple levels:
<Route path='/' component={Layout} />
<IndexRoute component={MyComponent} />
<Route path='/users' component={MyComponent}>
<Route path='user/:id' component={MyComponent} />
<Route path='*' component={UsersNotFound} />
</Route>
<Route path='/settings' component={MyComponent} />
<Route path='*' exact={true} component={GenericNotFound} />
</Route>
For react-router 4 and 5
Keep the path
<Switch>
<Route exact path="/users" component={MyComponent} />
<Route component={GenericNotFound} />
</Switch>
Redirect to another route (change url)
<Switch>
<Route path="/users" component={MyComponent} />
<Route path="/404" component={GenericNotFound} />
<Redirect to="/404" />
</Switch>
The order matters!
In newer versions of react-router you want to wrap the routes in a Switch which only renders the first matched component. Otherwise you would see multiple components rendered.
For example:
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router,
Route,
browserHistory,
Switch
} from 'react-router-dom';
import App from './app/App';
import Welcome from './app/Welcome';
import NotFound from './app/NotFound';
const Root = () => (
<Router history={browserHistory}>
<Switch>
<Route exact path="/" component={App}/>
<Route path="/welcome" component={Welcome}/>
<Route component={NotFound}/>
</Switch>
</Router>
);
ReactDOM.render(
<Root/>,
document.getElementById('root')
);
With the new version of React Router (using 2.0.1 now), you can use an asterisk as a path to route all 'other paths'.
So it would look like this:
<Route route="/" component={App}>
<Route path=":area" component={Area}>
<Route path=":city" component={City} />
<Route path=":more-stuff" component={MoreStuff} />
</Route>
<Route path="*" component={NotFoundRoute} />
</Route>
This answer is for react-router-4.
You can wrap all the routes in Switch block, which functions just like the switch-case expression, and renders the component with the first matched route. eg)
<Switch>
<Route path="/" component={home}/>
<Route path="/home" component={home}/>
<Route component={GenericNotFound}/> {/* The Default not found component */}
</Switch>
When to use exact
Without exact:
<Route path='/home'
component = {Home} />
{/* This will also work for cases like https://<domain>/home/anyvalue. */}
With exact:
<Route exact path='/home'
component = {Home} />
{/*
This will NOT work for cases like https://<domain>/home/anyvalue.
Only for https://<url>/home and https://<domain>/home/
*/}
Now if you are accepting routing parameters, and if it turns out incorrect, you can handle it in the target component itself. eg)
<Route exact path='/user/:email'
render = { (props) => <ProfilePage {...props} user={this.state.user} />} />
Now in ProfilePage.js
if(this.props.match.params.email != desiredValue)
{
<Redirect to="/notFound" component = {GenericNotFound}/>
//Or you can show some other component here itself.
}
For more details you can go through this code:
App.js
ProfilePage.js
For those who are using react router v6
Redirect component has been removed from the react-router version 6.
For react-router-dom v6, simply replace Redirect with Navigate
Migrating up to v6
npm install react-router-dom#6
import {Routes, Route, Navigate } from "react-router-dom";
function App() {
return (
<div>
<Routes>
<Route path="/404" element={<div>Choose the correct path/div>} />
<Route path="*" element={<Navigate replace to="/404" />} />
</Routes>
</div>
);
}
According to the documentation, the route was found, even though the resource wasn't.
Note: This is not intended to be used for when a resource is not found. There is a difference between the router not finding a matched path and a valid URL that results in a resource not being found. The url courses/123 is a valid url and results in a matched route, therefore it was "found" as far as routing is concerned. Then, if we fetch some data and discover that the course 123 does not exist, we do not want to transition to a new route. Just like on the server, you go ahead and serve the url but render different UI (and use a different status code). You shouldn't ever try to transition to a NotFoundRoute.
So, you could always add a line in the Router.run() before React.render() to check if the resource is valid. Just pass a prop down to the component or override the Handler component with a custom one to display the NotFound view.
The above answers are correct and for react 5 before. In React v6, Switch no longer exists. This solution is for react v6:
import {BrowserRouter as Router, Routes, Route, Link} from "react-router-dom";
...
<Router>
<ul>
<li>
<Link to="t1">component1</Link>
</li>
<li>
<Link to="t2">component2</Link>
</li>
</ul>
<Routes>
<Route path="/t1" exact element={<Component1/>}/>
<Route path="/t2" exact element={<Component2/>}/>
<Route path="*" element={<NotFound/>}/>
</Routes>
</Router>
I just had a quick look at your example, but if i understood it the right way you're trying to add 404 routes to dynamic segments. I had the same issue a couple of days ago, found #458 and #1103 and ended up with a hand made check within the render function:
if (!place) return <NotFound />;
hope that helps!
React Router v6
Live Demo: Redirect Default or 404 Routes with React Router
Example code:
<Router>
<Routes>
<Route path="users" element={<Users />} />
<Route path="posts" element={<Posts />} />
</Routes>
</Router>
To redirect and navigate to one of our chosen routes, we can use <Navigate> component from React Router. Now we can declare below our route configuration the case for empty routes, like this:
<Router>
<Routes>
<Route path="users" element={<Users />} />
<Route path="posts" element={<Posts />} />
<Route path="" element={<Navigate to="/users" />} />
</Routes>
</Router>
I had similar issue, instead of using * wild identifier or Default Switch Component. We can simply just use Route Component without defining path.
example:
<Switch>
<Route path="/" component={Root} />
<Route path="/home" component={Home} />
<Route component={NotFoundPage} />
// Default Component To load If none of the path matches.
</Switch>

Categories

Resources