react router 4 switch showing two components - reactjs

I'm trying to split my routes into many files, to achieve this I have a central file called routes.
import React from "react";
import { Router, Route, Switch } from "react-router-dom";
import history from "./History";
import StreamRoutes from "./stream/StreamRoutes";
const Routes = props => {
return (
<React.Fragment>
<Router history={history}>
<Switch>
<Route path="/" exact component={props => <h1>hello world</h1>} />
<StreamRoutes />
</Switch>
</Router>
</React.Fragment>
);
};
export default Routes;
and then a route file for all the main component like so:
import React from "react";
import { Route } from "react-router-dom";
import StreamCreate from "./components/StreamCreate";
import StreamEdit from "./components/StreamEdit";
import StreamList from "./components/StreamList";
import StreamShow from "./components/StreamShow";
const StreamRoutes = props => {
return (
<React.Fragment>
<Route path="/streams" exact component={StreamList} />
<Route path="/streams/new" exact component={StreamCreate} />
<Route path="/streams/:id" exact component={StreamShow} />
<Route path="/streams/edit/:id" exact component={StreamEdit} />
</React.Fragment>
);
};
export default StreamRoutes;
this works except when I try to access "/streams/new" or "/streams/:id", in any of those cases the router show both components at once.
I would like to know how to fix this or a better way to organize my routes would be highly appreciated.

It is possible to use a regex like FuzzyTree suggest, but this can get messy in a bigger project. I would suggest replacing React.Fragment in your StreamRoutes with Switch. This way it works like you'd expect.

Related

How to add multiple pages with React & Redux

I'm coding a MediumBlog like application and I'm using React & redux and Django Rest Framework for my Backend. I already made the first page, which is the first page you see when you go to https://medium.com/
I'm able to navigate to different categories without refreshing the page, which is the Main Purpose of React. But now, when you want to read a post in the medium blog, the page is refreshing. I have no idea how to do it with react. I know how to build a Single Page App, but here, it seems that a new page is being loaded when you click on a post. My question is :
.Is this loading a new page instead of a new route ( a new HTML file or something )
.How can I handle this " multipage " structure with react
Here is my Single Page App that can load content from different categories
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './css/App.css';
import { Provider } from 'react-redux';
import { store } from '../store.js';
import AppHeader from './header/AppHeader';
import HeaderCategories from './header/HeaderCategories'
import PostListStream from './posts/PostListStream';
import PostList from './posts/PostList';
import { BrowserRouter } from "react-router-dom";
import { Route, Switch } from 'react-router-dom';
class App extends Component {
render(){
return(
<Provider store={store}>
<div className="App">
<AppHeader />
<HeaderCategories />
<Switch>
<Route exact
key="sciences"
path="/sciences"
render={() => <PostListStream field="sciences" />}
/>
<Route exact
key="littérature"
path="/littérature"
render={() => <PostListStream field="littérature" />}
/>
<Route exact
key="sciences-sociales"
path="/sciences-sociales"
render={() => <PostListStream field="sciences-sociales" />}
/>
</Switch>
<PostList />
</div>
</Provider>
)
}
}
ReactDOM.render(<BrowserRouter>
<App />
</BrowserRouter>, document.getElementById('app'));
Here is where my are :
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './css/App.css';
import { Provider } from 'react-redux';
import { store } from '../store.js';
import AppHeader from './header/AppHeader';
import HeaderCategories from './header/HeaderCategories'
import PostListStream from './posts/PostListStream';
import PostList from './posts/PostList';
import { BrowserRouter } from "react-router-dom";
import { Route, Switch } from 'react-router-dom';
class App extends Component {
render(){
return(
<Provider store={store}>
<div className="App">
<AppHeader />
<HeaderCategories />
<Switch>
<Route exact
key="sciences"
path="/sciences"
render={() => <PostListStream field="sciences" />}
/>
<Route exact
key="littérature"
path="/littérature"
render={() => <PostListStream field="littérature" />}
/>
<Route exact
key="sciences-sociales"
path="/sciences-sociales"
render={() => <PostListStream field="sciences-sociales" />}
/>
</Switch>
<PostList />
</div>
</Provider>
)
}
}
ReactDOM.render(<BrowserRouter>
<App />
</BrowserRouter>, document.getElementById('app'));
Thank you so much
You can create dynamic routes to your posts using react-router-dom in a similar way to what you have done with your other routes. react-router doesn't really refresh the page, although it looks like it. It simply renders another component when the route changes.
To create dynamic routes for your posts you can do something like this:
<Route
path="/post/:postId"
render={() => <PostComponent />}
/>
or like this if you want it to be a little more readable:
<Route
path="/post/:postId"
component={PostComponent}
/>
:postId here is a dynamic id, whatever is placed after /post/ in your URL will be considered the postId by react-router.
Inside your PostComponent you can do something like this to fetch your postId variable:
import { withRouter } from "react-router-dom";
const PostComponent = withRouter(props => (
const postId = props.match.params.postId;
return ();
))
You can then use your postId to fetch your post from the backend and do whatever you need to do with it. As soon as you go to your post URL now, it will show the PostComponent without hard refreshing the page.

Cannot refresh or writing manually the url with react-router

I used BrowserRouter with his basename, my server is WINSCP, the routes works correctly but, when I refresh it or writing it manually, I get :
My App.js is :
import React, { Component } from 'react';
import { Route, Switch, BrowserRouter} from "react-router-dom";
import { BackTop } from 'antd';
import Header from './components/Header/Header';
import Agenda from './components/Agenda/Agenda';
import Planning from './components/Planning/Planning';
import CreerActivite from './components/CreerActivite/CreerActivite';
import TypesRDV from './components/TypesRDV/TypesRDV';
class App extends Component {
render() {
return (
<div>
<BrowserRouter basename="/ReactCalendar">
<Header/>
<Switch>
<Route exact path="/" component={Planning} />
<Route exact path="/creerActivite" component={CreerActivite} />
<Route exact path="/typesRDV" component={TypesRDV} />
</Switch>
</BrowserRouter>
<BackTop />
</div>
);
}
}
export default App;
On my package.json, I have "homepage": "https://dev/ReactCalendar" and my folder on WINSCP is /dev/ReactCalendar/
How can I fix it ?
The reason why this is happening is your server does not know what to serve when you hit that URL. There are multiple approaches to solving your problem. I'll suggest the easiest approach here.
Replace BrowserRouter with HashRouter.
class App extends Component {
render() {
return (
<div>
<HashRouter basename="/ReactCalendar">
<Header/>
<Switch>
<Route exact path="/" component={Planning} />
<Route exact path="/creerActivite" component={CreerActivite} />
<Route exact path="/typesRDV" component={TypesRDV} />
</Switch>
</HashRouter>
<BackTop />
</div>
);
}
}
And obviously, don't forget to import HashRouter from 'react-router-dom'.
You can view other approaches here:
React-router urls don't work when refreshing or writing manually

React Router not rendering component at path, returns blank page with correct pathing

React-Router appears to be working in my app except for the fact that I am getting a blank page instead of my component, even though it is directed to the proper path.
I'm scanning the documentation but I can't resolve the issue on my own after looking it over and searching Google/this site.
I had tried...
Making it so that the router.js file just contained the routes only to get the same results. Specifying exact path as well when doing so.
Reinstalling react-router-dom into the component in case there was an error when it downloaded.
Removing the provider in case that was the issue
Placing the code in the router file directly in the App.js file between the provider component tags
These are the files involved.
Router.js
import React from 'react';
import {Route, Switch, Redirect} from 'react-router-dom';
import LandingPage from '../scenes/LandingPage';
import CityPage from '../scenes/CityPage';
const Router = () => {
return (
<Switch>
<Redirect from='/' to='/landing' />
<Route path='/landing' component={LandingPage} />
<Route path='/citypage' component={CityPage} />
</Switch>
);
}
export default Router;
App.js
import React from "react";
import { BrowserRouter } from "react-router-dom";
import Router from "./services/Router";
import ChosenCityContextProvider from "./services/context/ChosenCityContext";
const App = () => {
return (
<BrowserRouter>
<ChosenCityContextProvider>
<Router />
</ChosenCityContextProvider>
</BrowserRouter>
);
};
export default App;
No error messages accompany the rendering of the site. Aside from the blank page, everything else appears to be working. In the React Dev tools, it states that the Router.Consumer has an object which is revealed to empty when expanded.
What is wrong with my code?
https://codesandbox.io/s/youthful-maxwell-rch1k?fontsize=14
Above is sandbox of code. I have the same issue here
I'm not certain why exactly this fixes the issue, but I've run into this on a work project so knew it worked.
If you add exact into the redirect element it forces the correct behavior.
import React from 'react';
import {Route, Switch, Redirect} from 'react-router-dom';
import LandingPage from '../scenes/LandingPage';
import CityPage from '../scenes/CityPage';
const Router = () => {
return (
<Switch>
<Redirect exact from='/' to='/landing' />
<Route path='/landing' component={LandingPage} />
<Route path='/citypage' component={CityPage} />
</Switch>
);
}
export default Router;
I tried this and it worked. I'm not sure why before it didn't. If anyone has an explanation please let me know because I am trying to learn what I did wrong initially.
<Route render={() => <Redirect from='/' to='/landing' />} />
I added the above, so my router file looked like this.
import React from 'react';
import {Route, Switch, Redirect} from 'react-router-dom';
import LandingPage from '../scenes/LandingPage';
import CityPage from '../scenes/CityPage';
const Router = () => {
return (
<Switch>
<Route path='/landing' component={LandingPage} />
<Route path='/citypage' component={CityPage} />
<Route render={() => <Redirect from='/' to='/landing' />} />
</Switch>
);
}
export default Router;
#DLowther has also showed me another solution
import React from "react";
import { Route, Switch, Redirect } from "react-router-dom";
import Page from "./Page";
import Home from "./Home";
const Router = () => {
return (
<Switch>
<Redirect exact from="/" to="/home" />
<Route path="/home" component={Home} />
<Route path="/page" component={Page} />
</Switch>
);
};
export default Router;
I would like to credit this individual for answering my question

React router doesn't render route, but path variables are set

I was trying to use my React router to render a companent based on the path in the browser. The path gets set correctly and the {this.props.location} object returns the right path. yet my router doesn't seem to be working.
Router file
import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import Infopage from './setSubscriptionPage';
import TrialRequestForm from './trialRequestForm';
import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
class SubscriptionActions extends React.Component {
render() {
console.log(this.props.location);
return (
<React.Fragment>
make a decission
<Router history={history}>
<div>
<Route exact path='/trial' Component={TrialRequestForm} />
<Route exact path='/teams' Component={Infopage} />
</div>
</Router>
</React.Fragment>
);
}
}
export default SubscriptionActions;
browser:
any hints would be appreciated. Cheers!
Your problem is here,
<Route exact path='/trial' Component={TrialRequestForm} />
You wrote Component with capital C which should be component small c.
<Route exact path='/trial' component={TrialRequestForm} />

React router changes url but not view

I am having trouble changing the view in react with routing. I only want to show a list of users, and clicking on each user should navigate to a details page. Here is the router:
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from 'react-router-dom';
import Users from "./components/Users";
import { Router, Route } from "react-router";
import Details from "./components/Details";
ReactDOM.render((
<BrowserRouter>
<div>
<Route path="/" component={Users} />
<Route path="/details" component={Details} />
</div>
</BrowserRouter>
), document.getElementById('app'))
When I use the url /details my browser navigates to that url, but does not change the view. Any other route throws 404 so it seems to recognize the route but not update.
You need to specify the attribute exact for your indexRoute, otherwise for even /details route it will still match with / . Also try to import Route from react-router-dom
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route } from 'react-router-dom';
import Users from "./components/Users";
import Details from "./components/Details";
ReactDOM.render((
<BrowserRouter>
<div>
<Route exact path="/" component={Users} />
<Route path="/details" component={Details} />
</div>
</BrowserRouter>
), document.getElementById('app'))
UPDATE:
Another thing that you need to do is to attach your component Users with withRouter. You need to make use of withRouter only when your component is not receiving the Router props,
This may happen in cases when your component is a nested child of a component rendered by the Router or you haven't passed the Router props to it or when the component is not linked to the Router at all and is rendered as a separate component from the Routes.
In Users.js add
import {withRouter} from 'react-router';
.........
export default withRouter(Users)
DOCS
You just have to wrap the components inside withRouter.
<Route exact path="/mypath" component={withRouter(MyComponent)} />
Here is a sample App.js file:
...
import { BrowserRouter as Router, Route, Switch, withRouter } from "react-router-dom";
import Home from "./pages/Home";
import Profile from "./pages/Profile";
class App extends Component {
render() {
return (
<Router>
<Switch>
<Route exact path="/" component={withRouter(Home)} />
<Route exact path="/profile" component={withRouter(Profile)} />
</Switch>
</Router>
);
}
}
export default App;
Additional
If you are using react router, componentWillReceiveProps will get called whenever the url changes.
componentWillReceiveProps(nextProps) {
var currentProductId = nextProps.match.params.productId;
var nextProductId = nextProps.match.params.productId; // from the new url
...
}
Note
Alternatively, you may also wrap the component in withRouter before exporting, but then you have to ensure a few other things. I usually skip this step.
export default withRouter(Profile)
I had the same issue and discovered that it was because I had a nested router. Once I removed the nested router, and simply put my component-specific routes within a switch component--the issue was resolved without having to use withRouter or make any additional changes.
<Router> // <--Remove nested Router
<Switch>
<Route exact path="/workflows" component={ViewWorkflows} />
<Route path="/workflows/new" component={NewWorkflow} />
</Switch>
</Router>
Yusufbek is describing a similar issue. I think it's a lot cleaner to store the component related routes at a view level versus storing all of them in one main router. In a production app, that's going to be way too many routes to easily read through and debug issues.
React Router v5 doesn't work with React 18 StrictMode
https://github.com/remix-run/react-router/issues/7870
I have faced the same problem but I fixed it. I have placed the home page as the last. It works for me. Just like below.
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from 'react-router-dom';
import Users from "./components/Users";
import { Router, Route } from "react-router";
import Details from "./components/Details";
ReactDOM.render((
<BrowserRouter>
<div>
<Route path="/details" component={Details} />
<Route path="/" component={Users} />
</div>
</BrowserRouter>
), document.getElementById('app'))
I had a similar issue but with different structure. I've added one Router that will handle all routes, I've used Switch component to switch views. But actually, it didn't. Only URL changed but not view. The reason for this was the Link component used inside of SideBar component which was outside of the Router component. (Yes, I've exported SideBar with "withRouter", not worked).
So, the solution was to move my SideBar component which holds, all Link components into my Router.
The problem is in my linkers, they are outside of my router
<div className="_wrapper">
<SideBar /> // Holds my all linkers
<Router>
<Switch>
<Route path="/" component={Home} />
<Route path="/users" component={Users} />
</Switch>
</Router>
</div>
Solution was moving my linkers into my router
<div className="_wrapper">
<Router>
<SideBar /> // Holds my all linkers
<Switch>
<Route path="/" component={Home} />
<Route path="/users" component={Users} />
</Switch>
</Router>
</div>
I had the same issue with react-router-dom 5
The problem was caused by the history package.
The version I was using was 5.0.0 but they don't work together.
Fixed by downgrading history to 4.10.1
Related issue: https://github.com/ReactTraining/history/issues/804
BrowserRouter fails to maintain history in your case. Use "Router" instead, Usage of this with custom history as props may help resolve your problem.
import {Router, Route, Switch, withRouter } from "react-router-dom";
import Home from "./pages/Home";
import Profile from "./pages/Profile";
import {createBrowserHistory} from 'history';
export const customHistory = createBrowserHistory(); //This maintains custom history
class App extends Component {
render() {
return (
<Router history={customHistory}>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/profile" component={Profile} />
</Switch>
</Router>
);
}
}
export default App;
Then in your components, import customHistory from 'App' and use that to navigate.
customHistory.push('/pathname');
Hope This help! :)
When using Redux and I had similar issues where the url was updating in the address bar but the app was not loading the respective component. I was able to solve by adding withRouter to the export:
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
export default withRouter(connect(mapStateToProps)(MyComponentName))
According to this issue here, react-router-dom isn't compatible with React 18 because BrowserRouter is a child of StrictMode.
So to resolve the issue.
Instead of this:
<React.StrictMode><BrowserRouter>...</BrowserRouter></React.StrictMode>
Do this:
<BrowserRouter><React.StrictMode>...</React.StrictMode></BrowserRouter>
It worked for me this way, I hope it helps.
In my case, I'd mistakenly nested two BrowserRouters.
You need to add exact to the index route and rather than enclosing those Route components by div, use Switch from react-router-dom to switch between the routes.
import React from "react";
import ReactDOM from "react-dom";
import { Route, Switch } from 'react-router-dom';
import Users from "./components/Users";
import Details from "./components/Details";
ReactDOM.render((
<div>
<Switch>
<Route path="/" component={Users} exact/>
<Route path="/details" component={Details} />
</Switch>
</div>
), document.getElementById('app'))
I Tried adding "exact" in front of the home path
like this
<Route exact path="/" component={Home}></Route>
It is working fine...
I had similar issue with React Router version 4:
By clicking <Link /> component, URL would change but views wouldn't.
One of views was using PureComponent rather than Component (imported from react) and that was the cause.
By replacing all route rendered components that were using PureComponent to Component, my issue was resolved.
(Resolution source: https://github.com/ReactTraining/react-router/issues/4975#issuecomment-355393785)
None of the answers here solved my issue, turns out I had to add a dependency to my useEffect hook. I had something like this:
App.js
<Route
path="/product/:id"
component={MyComponent}
/>
MyComponent.jsx
const { id } = useParams();
useEffect(() => {
fetchData();
}, []);
I had a button to change to another product, which would only update the :id on the url, I could see the url changed, but no effect on the page. This change fixed the issue:
MyComponent.jsx
const { id } = useParams();
useEffect(() => {
fetchData();
}, [id]); // add 'id' to dependency array
Now when the id changes, it trigger a function to update the data and works as expected.
Hmm there no any SWITCH to actually switch views.
this is how i use router to switch from landin page to main site
//index.jsx
ReactDOM.render( (<BrowserRouter><App/></BrowserRouter>), document.getElementById('root') );
//App.jsx
render()
{
return <div>
<Switch>
<Route exact path='/' component={Lander}/>
<Route path='/vadatajs' component={Vadatajs}/>
</Switch>
</div>
}
https://jsfiddle.net/Martins_Abilevs/4jko7arp/11/
ups i found you use different router ..sorry then maybe this fiddle be for you useful
https://fiddle.jshell.net/terda12/mana88Lm/
maybe key of solution is hiden in line for main render function ..
Router.run(routes, function(Handler) {
React.render(<Handler />, document.getElementById('container'));
});
I was facing similar issue I resolve to like this please have a look I hope it's working.
You need to use componentWillReceiveProps function in your component.
clicked a link first time by calling url www.example.com/content1/ componentDidMount() is run.
Now when you click another link say www.example.com/content2/ same component is called but this time prop changes and you can access this new prop under componentWillReceiveProps(nextProps) which you can use to call API Or make state blank and get new data.
componentWillReceiveProps(nextProps){
//call your API and update state with new props
}
For me, I had:
export MyClass extends React.Component
with:
export default withRouter(MyClass)
Meanwhile, in my App.js, I had:
import { MyClass } from './MyClass'
Those playing the home game can see my problem. I was not getting the import with the Router passed into the child classes. To clean this up, I moved the withRouter call into the Route component declaration:
<Router exact path={'/myclass'} component={withRouter(MyClass)} />
Back in MyClass, I changed it to a default export:
export default MyClass extends React.Component
And then finally, in App.js, I changed my import to:
import MyClass from './MyClass'
Hopefully this helps someone. This ensures I didn't have two ways to export the same class, thus bypassing the withRouter prepend.
I also had the same problem. Although it is not a very effective solution, I solved it with a cunning method.
The ComponentDidMount method works every time our url changes.
Within the method we can compare the previous url with the current url and we can the state change or page refresh.
componentDidUpdate(prevProps) {
if (this.props.match.url !== prevProps.match.url) {
//this.props.history.go(0) refresh Page
//this.setState(...) or change state
}
}
<Route exact path="/" component={Users} />
<Route exact path="/details" component={Details} />
I was also facing the same issue which was resolved using the exact attribute. try to use the exact attribute.
I met trouble too.
https://github.com/chengjianhua/templated-operating-system
And I have tried the solutions metioned by Shubham Khatri, but It doesn't work.
I solved this problem, maybe can help you.
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md
According the above guide document, when you use PureComponent or use with state management tools like redux, mobx ... It may block the update of your route. Check your route component, ensure you did't block the rerender od your component.
You should check this out: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md
Therefore it's definitely not Users or Details, because they are directly rendered by <Route>, and the location will get passed to props.
I am wondering, why do you need the <div> between <BrowserRouter> and <Route>? Remove that and let me know if it works.
I had a similar issue with a conditional Layout:
class LayoutSwitcher extends Component {
render () {
const isLoggedIn = this.props.isLoggedIn
return (
<React.Fragment>
{ isLoggedIn
? <MainLayout {...this.props} />
: <Login />
}
</React.Fragment>
)
}
}
and rewrote the conditions like so:
render () {
const isLoggedIn = this.props.isLoggedIn
if (isLoggedIn) {
return <MainLayout {...this.props} />
}
return <Login />
}
This solved it. In my case, it seems that the context was lost.
I get the same Issue.
I don't think that in this case he needs to add the prop withRouter,
just check in your NavLink you write the good path name as details.
for the route try to start from the specific route to the general one like
<Route path="/details" component={Details} />
<Route path="/" component={Users} />
in your NavLink it should be something like this
<NavLink className="nav-link" to="/details">
details<span className="sr-only">(current)</span>
</NavLink>
a remarque for the import its better to start by importing all stuff related to React after that import the other module
like this one:
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from 'react-router-dom';
import Users from "./components/Users";
import { Router, Route } from "react-router";
import Details from "./components/Details";
come like this:
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from 'react-router-dom';
import { Router, Route } from "react-router";
import Users from "./components/Users";
import Details from "./components/Details";
In my case, switching to HashRouter instead of BrowserRouter solved my issue
I was accidentally using a BrowserRouter around my Link's.
<BrowserRouter>
<div>
<Link to="/" component={Users} />
<Link to="/details" component={Details} />
</div>
</BrowserRouter>
If you just started having this issue recently, take a look at https://github.com/remix-run/react-router/issues/7415
The issue is with react-router-dom 5+ and the history dependency.
If you installed it separately using yarn install history you need to uninstall it, do yarn install history#4.10.1
In my case it wasn't working because I imported Browser Router as Router, Like This:
<Router>
<div> <Navbar/></div>
<Routes>
<Route exact path="/" element={<Pageone/>}></Route>
<Route path="/home" element={<Home/>}></Route>
<Route path="/about" element={<About/>}></Route>
<Route path="/contact" element={<Contact/>}></Route>
</Routes>
<div><Footer /></div>
</Router>
</div>
Then It was fixed by adding BrowserRouter instead:
<BrowserRouter>
<div> <Navbar/></div>
<Routes>
<Route exact path="/" element={<Pageone/>}></Route>
<Route path="/home" element={<Home/>}></Route>
<Route path="/about" element={<About/>}></Route>
<Route path="/contact" element={<Contact/>}></Route>
</Routes>
<div><Footer /></div>
</BrowserRouter>
</div>
Hope this helps someone!
Try this,
import React from "react";
import ReactDOM from "react-dom";
import Users from "./components/Users";
import { Router, Route } from "react-router";
import Details from "./components/Details";
ReactDOM.render((
<Router>
<Route path="/" component={Wrapper} >
<IndexRoute component={Users} />
<Route path="/details" component={Details} />
</Route>
</Router>
), document.getElementById('app'))
I had the same issue and I fixed it importing the history from the #types folder in node_modules. I imported it from npm (npm i #history)

Resources