I'm understanding the react-router-dom library and the components that provide the routing and noticed a strange behavior as below.
Assume we have our App.js component which return JSX code as below.
import './App.css';
import ColorAdder from './ColorAdder';
import {Switch,Route} from 'react-router-dom';
function App() {
return (
<Switch>
<ColorAdder/>
<Route path="/hello" exact>
<p>Hello</p>
</Route>
</Switch>
);
}
export default App;
Now the component ColorAdder.js also consists of Routes with Path and exact attributes declared as below.
import { Route } from "react-router-dom";
const ColorAdder = ()=>{
return (
<Route path="/hi" exact>
<p>Hi</p>
</Route>
)
}
export default ColorAdder;
The App.js is wrapped with <BrowserRouter> and what i noticed is when we test the page with url http://localhost:3002/hi it returned Hi as expected but it does n't return anything for 'http://localhost:3002/hello'.why does it behave this way ? Can anyone please explain this behavior why the Route with path hello is not identified even when it is under Switch wrapping and with Path and exact attributes
Below is the Index.js using BrowserRouter
ReactDOM.render(<BrowserRouter><App/></BrowserRouter>,document.getElementById('render'))
Versions:
react-router-dom#5,
npm - 7.20.3,
node - v16.7.0
first, its good practice to have routing logic in app.js plus don't render <ColorAdder/> inside the switch instead use the component attribute in like this:-
your App.js : -
import './App.css';
import ColorAdder from './ColorAdder';
import {Switch,Route} from 'react-router-dom';
function App() {
return (
<Switch>
<Route path="/hi" component={colorAdder} exact></Route>
// if you have other routes do like this
<Route path="/hello" component={<p>hello</p>} exact></Route>
// instead of hard coding <p>hello </p> here you can create a separate component that render the helloComponent
</Switch>
);
}
your ColorAdder.js :- it should only return the content cuz the routing logic is done in app.js
import { Route } from "react-router-dom";
const ColorAdder = ()=>{
return (
<p> h1 </p>
)
}
export default ColorAdder;
NB :- BrowserRouter should be inside index.js it should wrap the app component
<BrowserRouter>
<App />
</BrowserRouter>,
Related
import { BrowserRouter } from 'react-router-dom';
import Header from './Components/Header';
function App() {
return (
<BrowserRouter>
<div>
<Header />
</div>
</BrowserRouter>
);
}
I wanted to display the contents on the Header page, but when it's wrapped in the BrowserRouter tag it gives the output as empty in the webpage. When it's not wrapped the content if the page displays. I have updated the react-router-dom to it's latest version.
I think you might actually be using react-router-dom#6. The Switch component was replaced by a Routes component and the Route component API changed significantly.
Example:
import { Routes, Route } from 'react-router-dom';
export default function App() {
return (
<Routes>
<Router path="/" element={<Home />} />
... other routes ...
</Routes>
);
}
Refer this link
Hi i am new in coding and trying to build a react app. I just create a react component named login.js and export it as default and import it in app.js, but as i start my react app the div that i written in login.js is not displaying on the display.
Login.js code:
import React from 'react';
// import styled from 'styled-components';
const Login = ( props ) => {
return(
<div>hello world</div>
);
};
export default Login;
App.js code:
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import './App.css';
import Login from './components/Login';
function App() {
return (
<div className="App">
<Router>
<Routes>
<Route exact path='/'>
<Login />
</Route>
</Routes>
</Router>
</div>
);
}
export default App;
my app output:
output after running the app
it is exported because it showing this :
proof that the component is exported
I tried so many ways but nothing worked. Plz help
As in the document of react-router-dom, you will need to specify your component to a prop named element.
<Route exact path='/' element={<Login />} />
See more here
I was following a react tutorial on routing and how it works in react. I have no idea why this code isnt working as intended. If I understand correctly, It is supposed to render a component which I specify. Any help would be appreciated
ps: this is the exact same code used in the tutorial i was following and was working fine during it
App.js code
import React, { Component } from 'react';
import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
Link
} from "react-router-dom";
import Main from"./comp/Mainpage";
class App extends Component {
render() {
return (
<Router>
<Routes>
<Route exact path="/" component={Main}/>
</Routes>
</Router>
);
}
};
export default App;
Main page code:
import React from'react';
function Main(){
return (
<div>
<h1>Inside main page</h1>
</div>
);
};
export default Main;
Replace your route from component={Main} to element={<Main />}
<Route exact path="/" element={<Main />} />
I believe its a change in v6 react-router-dom - https://reactrouter.com/docs/en/v6/getting-started/overview
I'm a beginner in React and I'm learning how to route. My page changes urls, but the view turns to a blank page. My index page is working fine. I don't have any console errors.
In Router.js
import React from "react";
import {
BrowserRouter,
Route,
Switch,
} from "react-router-dom";
import App from './App'
import SinglePriceGrid from './component/SinglePriceGrid'
function Router() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={App} />
<Route path="/single-page-grid" component={SinglePriceGrid} />
</Switch>
</BrowserRouter>
)
}
export default Router
In SinglePriceGrid.js
import React from 'react';
import '../css/SinglePriceGrid.css'
class SinglePriceGrid extends React.Component {
render() {
return (
<div className="SinglePriceGrid">
<h1>Hello Single Price Grid!</h1>
</div>
);
}
}
export default SinglePriceGrid;
edit: Added the imports I used
edit: Readded the Switch, but it did not solve the problem
keep the Routes inside Switch. Try this
import React from "react";
import {
BrowserRouter,
Route,
Switch,
} from "react-router-dom";
import App from './App'
import SinglePriceGrid from './component/SinglePriceGrid'
function Router() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={App} />
<Route path="/single-page-grid" component={SinglePriceGrid} />
</Switch>
</BrowserRouter>
)
}
export default Router
I'm really sorry, but the error is really humiliating. My path is wrong. I used "page" instead of "price" for the endpoint.
I am having a problem, I am using react router and I will explain the situation.
I have a form to log in, with /authenticate in the url, if authentication is successed then I go to "/" ( home page ) which is doing good now, and I have two navigation bars, one on the left, the other on the top, now when I click on the links, the url changes but the components are not rendered on clicking them, but if I tap the url on the browser and click enter ( page refreshed ) the component is rendered correctly.
Here is my code :
This is the component rendered after the succesful log in, it is my main application, so The MenuGauche and MenuTop are always rendered :
import React, { Fragment } from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import MenuGauche from "./MenuGauche";
import MenuTop from "./MenuTop";
import Acceuil from "./Acceuil";
import Roles from "./Roles";
const Application = () => {
return (
<Fragment>
<MenuGauche></MenuGauche>
<MenuTop></MenuTop>
<BrowserRouter>
<Switch>
<Route path="/" component={Acceuil}></Route>
<Route path="/roles" component={Roles}></Route>
</Switch>
</BrowserRouter>
</Fragment>
);
};
export default Application;
And here is my top route component ( the default component suggested by react ) :
function App(props) {
return (
<ThemeProvider theme={theme}>
<I18nProvider locale={props.language.language}>
<BrowserRouter>
<Switch>
<Route exact path="/" component={Application}></Route>
<Route path="/authenticate" component={Authentification}></Route>
</Switch>
</BrowserRouter>
</I18nProvider>
</ThemeProvider>
);
}
Why is that not working ? I would like to get any help to solve that, a solution or a proposition!
One solution is provided, it is that have to keep the BrowserRouter only in the top root component, but still nothing!
here is the modification :
import React, { Fragment } from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import MenuGauche from "./MenuGauche";
import MenuTop from "./MenuTop";
import Acceuil from "./Acceuil";
import Roles from "./Roles";
const Application = () => {
return (
<Fragment>
<MenuGauche></MenuGauche>
<MenuTop></MenuTop>
<Route path="/" component={Acceuil}></Route>
<Route path="/roles" component={Roles}></Route>
</Fragment>
);
};
export default Application;
If you feel like I need to provide more code just ask for it.
the problem is that you are using two BrowserRouter component, make sure that is is used once and it is in most top level component in whole application