react routing gives the blank screen not proper output - reactjs

I have try to call my routes form App.js file but this is not working properly. I have try many times but browser gets the blank screen. Am totally new to react js Framework
App.js:
import React from 'react';
import { BrowserRouter, Route,Routes} from 'react-router-dom';
import Login from './Components/Login/Login';
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route path="/" exact={true} component={Login}></Route>
<Route path="/login" exact={true} component={Login}></Route>
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
Login Component here:(Login.js)
import React from "react";
const Login = () => {
return (
<form>
<h3>Sign In</h3>
<div className="form-group">
<label>Email address</label>
<input
type="email"
className="form-control"
placeholder="Enter email"
/>
</div>
</form>
);
};
export default Login;

You should remove <Routes>
in React Router V5 we just have <BrowserRouter> and <Route>
OR also you can do this for importing: BrowserRouter as Router
import React from "react";
import {
BrowserRouter as Router,
Switch
Route,
} from "react-router-dom";
import Login from "./Login";
function App() {
return (
<div className="App">
<BrowserRouter>
<Switch>
<Route exact path="/" component={Login}></Route>
<Route path="/login" exact={true} component={Login}></Route>
</Switch>
</BrowserRouter>
</div>
);
}
export default App;

try this code snippet.
Change Routes with Switch Statement.
import React from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import Login from "./Login";
function App() {
return (
<div className="App">
<BrowserRouter>
<Switch>
<Route exact path="/" component={Login}></Route>
<Route path="/login" exact={true} component={Login}></Route>
</Switch>
</BrowserRouter>
</div>
);
}
export default App;

After a day of research, I found the solution and it`s nowhere on the internet.
type the following commands in the terminal:
npm uninstall react-router-dom (if you installed that way) Or
npm uninstall react-router-dom#6 (if you installed that way)
Then install react-router-dom#5 by typing
npm install react-router-dom#5
and you would be able to see the components in the browser

Related

React router doesn't work for some reason

I'm working on my portfolio. I'm trying to put all components (Home, Skills, Projects, Contact) on one page, so I'm not planning to set routes for those component. However I want to set a route for project detail component, which I'll put a link on Projects component. I also want to set a route for 404 page.
I wrote the following code but it doesn't know ProjectDetail page when I access http://localhost:3000/projects. Can you give me advice how I can fix the problem?
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from './Home';
import Skills from './Skills';
import Projects from './Projects';
import ProjectDetail from './ProjectDetail';
import Contact from './Contact';
import Error from './Error';
const App = () => {
return (
<>
<Router>
<Home />
<Skills />
<Projects />
<Contact />
<Routes>
<Route path='/projects' element={<ProjectDetail />} />
<Route path='*' element={<Error />} />
</Routes>
</Router>
</>
);
};
export default App;
I think you need to wrap all the components that u want in one page and put it on a route like this: <Route path="/" element={<OnePageApp/>}/>
After this i don't see any other errors.
Good Luck, I hope i have been able to help you
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from './Home';
import Skills from './Skills';
import Projects from './Projects';
import ProjectDetail from './ProjectDetail';
import Contact from './Contact';
import Error from './Error';
const App = () => {
return (
<>
<Router>
<Home />
<Skills />
<Projects />
<Contact />
<Routes>
<Route path='/projects' element={ProjectDetail} />
<Route path='*' element={Error} />
</Routes>
</Router>
</>
);
};
export default App;

Changed syntax in react-route-dom from 'Switch' to 'Routes' but still no display in the browser

I'm creating a Google Clone. This is my current code for that. I read that I needed to changed the syntax from 'Switch' to 'Routes', given the update for react-router. I did just that and my "This is the search page" is not displaying inside of the browser.
import React from "react";
import './App.css';
import Home from './pages/Home';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom"
function App() {
return (
// BEM
<div className="app">
<Router>
<Routes>
<Route path="/search">
<h1>This is the search page</h1>
</Route>
<Route path="/">
<Home />
</Route>
</Routes>
</Router>
</div>
);
}
export default App;
you need to create each component speratly some things like this:
import React from 'react;
function Home(){
return(
<div>
<h1>Home</h1>
</div>
);
}
export default Home;
import React from 'react;
function Search(){
return(
<div>
<h1>Search</h1>
</div>
);
}
export default Search;
change index.js to :
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById("root"));
change App.js to
import React, {useEffect} from 'react';
import ReactDOM from "react-dom";
import { BrowserRouter ,Routes,Route} from 'react-router-dom';
import Search from '.........'
import Home from '.......'
function App(){
return(
<BrowserRouter>
<Routes>
<Route exact path="/" element={<Home/>} />
<Route path="/search" element={<Search/>} />
<Routes>
</BrowserRouter>
);
}
export default App;
if(document.getElementById('app')){
ReactDOM.render(<App/>,document.getElementById('app'));
}
In react-router-dom#6 the Routecomponent API changed significantly. Thechildrenprop is only used for rendering nestedRoutecomponents. All routed content is now rendered on a singleelementprop taking aReactNode`, a.k.a. JSX.
Move the content from being wrapped into the element prop.
function App() {
return (
// BEM
<div className="app">
<Router>
<Routes>
<Route path="/search" element={<h1>This is the search page</h1>} />
<Route path="/" element={<Home />} />
</Routes>
</Router>
</div>
);
}
Route component expects 2 parameters path, and element.
Refer to the official doc here
function App() {
return(
<BrowserRouter>
<Routes>
<Route path="/" element={<Home/>} />
<Route path="/search" element={<Search/>} />
<Routes>
</BrowserRouter>
);
}

routed component not displaying in react

I have a simple react page with a navbar and a single route.
App.js:
import React from 'react';
import { BrowserRouter as Router, Route } from "react-router-dom";
import "bootstrap/dist/css/bootstrap.min.css";
import Navbar from "./components/navbar.component"
import CreateUser from "./components/create-user.component";
function App() {
return (
<Router>
<div className="container">
<Navbar />
<br />
<Route path="/user" component={CreateUser} />
</div>
</Router>
);
}
export default App;
create-user.component.js:
import React, { Component } from 'react';
export default class CreateUser extends Component {
render() {
return (
<div>
<p>You are on the Create User component!</p>
</div>
)
}
}
The navbar displays ok but if I add in the route the navbar disappears and the routed page doesn't work.
Try this:
<Route path="/user" element={<CreateUser />} />
downgrading react router dom "resolved" the issue, not ideal but at least it works now :)
npm uninstall react-router-dom && npm install react-router-dom#5.2.0
if you are using react-router-v6 then component don't support anymore this is the correct syntax
<BrowserRouter>
<div className="container">
<Navbar />
<br />
<Routes>
<Route path="/user" element={<CreateUser />} />
<Route path="*" element={<Navigate to="/user" />} />
</Routes>
</div>
</BrowserRouter>
i also recommend go through documentation first because there is quite changes in react router
React Router Documentation

React component makes a white page

import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Discussion from './discussion.js';
import Rules from './rules.js';
import Workflow from './workflow.js';
export default function() {
return (
<div>
hello
<Switch>
<Route exact path="/" component={Discussion} />
<Route exact path="/rules" component={Rules} />
<Route exact path="/workflow" component={Workflow} />
</Switch>
</div>
)
}
I'm importing the function called 'PageContent' from './page-content'
calling it with in app.js
Then the page goes blank

One router work but other router not work

Hey i have a reactjs app with this as app.tsx
import tw from 'twin.macro';
import { hot } from 'react-hot-loader/root';
import { Route, BrowserRouter as Router, Routes as Switch } from 'react-router-dom';
import Main from './Main';
import Test from './Test';
const App = () => {
return (
<><Main /><div css={tw`mx-auto w-auto`}>
<Router>
<Switch>
<Route path="/element" element={<Test />} />
<Route path="/" element={<Main />} />
</Switch>
</Router>
</div></>
);
};
export default hot(App);
But when i run a ./node_modules/.bin/webpack --watch --progress the localhost/ work but localhost/element not work i have a 404 error
Not Found The requested URL was not found on this server
Can you tell me why its not work?
(i use 6.2.1 of react-router-dom)
first and foremost you need to mention the version you are using , and try this if it works for u :-
your components should look like this, to work:-
import tw from 'twin.macro';
import { hot } from 'react-hot-loader/root';
import { Route, Routes} from 'react-router-dom';
import Main from './Main';
import Test from './Test';
const App = () => {
return (
<><Main /><div css={tw`mx-auto w-auto`}>
<Routes>
<Route exact path="/element" element= {<Test />} />
<Route exact path="/" element= {<Main/>} />
</Routes>
</div></>
);
};
NB: try to wrap your index.tsx component with the <BrowserRouter> </BrowserRouter> like this :
import { BrowserRouter } from "react-router-dom";
ReactDOM.render( <BrowserRouter> <App/> </BrowserRouter>, document.getElementById('root'))
therefore you can remove the BrowserRouter from the app.tsx
I think you're using ‘react-router-dom’ newer version. Switch is no longer in the new version. please read here
Switch' is not exported from 'react-router-dom'
import { Route, BrowserRouter as Router, Routes as Switch } from 'react-router-dom';
function App() {
const Main = () => {
return (
<h1>Main</h1>
)
}
const Test = <h1>Test</h1>
return (
<div >
<Router>
<Switch>
<Route path="/element" element={<Main />} />
<Route path="/" element={Test} />
</Switch>
</Router>
</div>
);
}
export default App;
You need to use the <Main /> structure

Resources