React router structure for dashboard component - reactjs

Hi i am new developer in ReactJS. I have a problem and want to learn how to make routing for dashboard. My root component is App component and it calls only dashboards of my pages. But I want to call different components inside of dashboard such as Login , Register etc. When I use "Link" in anywhere, How can I do this ? Could you help me at this issue?
index.tsx for BrowserRouter
import {BrowserRouter} from "react-router-dom";
const app =<App /> ;
const container = (
<>
<BrowserRouter>
{app}
</BrowserRouter>
</>
);
my router App.tsx component:
import { Route, Switch } from "react-router-dom";
import LoginDash from "../containers/login/LoginDash"
class App extends Component {
render() {
return (
<div className="App">
<Switch>
<Route path="/Login" exact component={LoginDash}></Route>
<Route path="/" exact component={LoginDash}></Route>
</Switch>
</div>
);
}
}
export default App;
My dashboard LoginDash.tsx:
import {BrowserRouter as Router, Route,Switch} from "react-router-dom"
import LoginPart from "../../components/login/left/LoginPart";
import RegisterPart from "../../components/login/left/RegisterPart";
import AnitamionPart from '../../components/login/right/AnimationPart';
export const Login = () => {
return (
<div className="login-container">
<div className="row login-row" >
<div className="login-left-part" >
<Router>
<Switch>
<Route exact path = "/Login" component={LoginPart}></Route>
<Route exact path = "/Register" component={RegisterPart}></Route>
</Switch>
</Router>
</div>
<div className=" login-right-part" >
<AnitamionPart></AnitamionPart>
</div>
</div>
</div>
)
}
export default Login;

First of all, you only need to use one instance of BrowserRouter in your App, Since you already wrap App component with BrowserRouter that would be enough
Secondly, if you specify exact attribute on Routes, no nested Routes will ever match. Make sure you don't use Route with exact prop if the component has any nested Route defined
Lastly, you only need to render LoginDash on / route, /Login handling can be done separately as a nested Route
Update your individual components like below
import {BrowserRouter} from "react-router-dom";
// Do not render app as a constant separetely,
const container = (
<>
<BrowserRouter>
<App />
</BrowserRouter>
</>
);
import { Route, Switch } from "react-router-dom";
import LoginDash from "../containers/login/LoginDash"
class App extends Component {
render() {
return (
<div className="App">
<Route path="/" component={LoginDash}></Route>
</div>
);
}
}
export default App;
export const Login = () => {
return (
<div className="login-container">
<div className="row login-row" >
<div className="login-left-part" >
<Switch>
<Route exact path = "/Login" component={LoginPart}></Route>
<Route exact path = "/Register" component={RegisterPart}></Route>
</Switch>
</div>
<div className=" login-right-part" >
<AnitamionPart></AnitamionPart>
</div>
</div>
</div>
)
}
export default Login;
Working demo

Related

A <Route> is only ever to be used as the child of <Routes> element, never rendered directly

I was trying to display my layout.jsx on the browser but keep getting "A <Route> is only ever to be used as the child of Routes element, never rendered directly;" error I tried wrapping it in Routes but keep getting the error.
import React from 'react'
import Sidebar from '../sidebar/Sidebar';
import Routes from '../Routes';
import {BrowserRouter, Route} from 'react-router-dom';
const Layout = () => {
return (
<BrowserRouter>
<Route render={(props) =>(
<div className='layout'>
<Sidebar {...props} />
<div className="layout__content">
<div className="layout__content-main">
<Routes />
</div>
</div>
</div>
)
} />
</BrowserRouter>
)
}
export default Layout
import React from 'react';
import {Route, Routes} from 'react-router-dom';
import Dashboard from '../pages/Dashboard';
import Customers from '../pages/Customers';
const RRoutes = () => {
return (
<Routes>
<Route path='/' component={Dashboard} />
<Route path='/customers' component={Customers}/>
</Routes>
)
}
export default RRoutes
import React from 'react'
const Sidebar = () => {
return (
<div>
Hello Sidebar
</div>
)
}
export default Sidebar
Since you are using react-router-dom#6 the Route component API changed quite a bit. There are no longer any component or render and children function props, they were replaced by a single element prop taking a ReactNode, a.k.a. JSX. Additionally, all Route components must be rendered by a Routes component, the spiritual successor to the v5 Switch component that handles route matching and rendering.
import React from 'react'
import {BrowserRouter, Route} from 'react-router-dom';
import Sidebar from '../sidebar/Sidebar';
import MyRoutes from '../Routes';
const Layout = () => {
return (
<BrowserRouter>
<div className='layout'>
<Sidebar />
<div className="layout__content">
<div className="layout__content-main">
<MyRoutes />
</div>
</div>
</div>
</BrowserRouter>
);
}
...
import React from 'react';
import { Route, Routes } from 'react-router-dom';
import Dashboard from '../pages/Dashboard';
import Customers from '../pages/Customers';
const MyRoutes = () => {
return (
<Routes>
<Route path='/' element={<Dashboard />} />
<Route path='/customers' element={<Customers />}/>
</Routes>
);
}
export default MyRoutes;

Unable to see the text returned from home and tutorials page in react-hooks site

Unable to see the header text returned in my Home and Tutorials page in my react hooks site. Below is my App.js, Navigation.js and Home.js. Could someone please advise about the problem here.
App.js
import React from 'react';
import { BrowserRouter, Route, Switch } from "react-router-dom";
import './App.css';
import Home from "./components/Home";
import Tutorials from "./components/Tutorials";
import Navigation from './components/Navigation';
function App() {
return (
<BrowserRouter>
<Navigation>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</Navigation>
</BrowserRouter>
);
};
export default App;
Navigation.js
import React from 'react';
import { NavLink} from 'react-router-dom';
const Navigation = () => {
return (
<div className="App">
<div className="wrapper">
<div id="wrap">
<nav className="siteNavigation_nav_links">
<div className="row" ></div>
<div className="main_links_nav">
<NavLink className="mob_link" to="/">Home</NavLink>
<NavLink className="mob_link" to="/tutorials">Tutorials</NavLink>
</div>
</nav>
</div>
</div>
</div>
)
}
export default Navigation;
Home.js
import React from 'react';
const Home = () =>{
return (
<div className="App">
<h1>This is Home Page !</h1>
</div>
)
}
export default Home;
Tutorials.js
import React from 'react';
const Tutorials = () =>{
return (
<div><h1>This is Tutorials Page !</h1></div>
)
};
export default Tutorials;
Navigation is a Navbar component. And you want it everywhere on the site.
So, you can easily do it by using it outside the scope of the Switch component.
In doing so, it will render all over your screens.
<BrowserRouter>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</BrowserRouter>

Problem rendering Component in a different page with React Router

I'm creating a personal website to train with React and i have been stuck since a couples of days to render a component with React Router like i will render a blog post when i click on the card.
Eveytime i click on the card instead i got the correct data but it's om the bottom of the main page and i would like to open it dynamically on a new page, like if i click on an article of a blog or a news.
My card component
render() {
const { match } = this.props;
const { data, value } = this.state;
return (
<>
<div>
{data.map((job, id) => (
<div key={id}>
<div key={job._id} className="blog-card">
<div className="meta">
<div className="photo">
<img src={job.img} alt="logo" />{" "}
</div>
</div>
<div className="description">
<p> {job.description}</p>
<p className="read-more">
<p>{job.location}</p>
<p>
<span className="apply-job">
{" "}
<Link
className="link-apply"
to={{
pathname: `${match.url}/${job._id}`,
state: job
}}
>
go to {job.workplace_name}
</Link>{" "}
</span>
</p>
</p>
</div>
</div>
</div>
))}
</div>
}
<Route path={`${match.path}/:_id`} component={Articles} />
</>
);
}
}
When i click on go to {job.workplace_name} i would like to render the component below on a new page and not under my card component
const Articles = ({ location }) => (
<div>
<h1>Hello</h1>
<h1>{location.state.workplace_name}</h1>
<h2>{location.state.position_name}</h2>
<h2>{location.state.description}</h2>
<h2>{location.state.Compensation}</h2>
</div>
)
export default Articles;
When you code your app with react-router you should have a top-level component that is in charge of deciding which screen to print based on the url you are visiting.
It should look like this
import { BrowserRouter } from "react-router-dom";
import { Route, Switch } from "react-router";
const App = () => (
<Switch>
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route path="/invoices/:id" component={Invoice} />
</Switch>
);
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
el
);
The Switch component will render only the first Route that matches the current URL.
If you don't use it, every route that match will be rendered.
The is useful at the top level to avoid having two screens at the same time (kinda like your issue).
Your biggest issue is that you put the route responsible for the display of a blog page inside your card component.
Your component hierarchy:
Router
└ App
└ Route+Home (maybe)
└ Card
└ Route+Articles
What it should be instead:
Router
└ App
└ Switch
├ Route+Home (maybe)
│ └ Card
└ Route+Articles
Also when you new to change the location (url) you can use the Link component as you did, or use history props that you get from the Route component.
In the example above, Dashboard and Invoice get the "history" property.
history.push(path) will simulate navigation
history.replace(path) will simulate redirection.
and there's more https://reacttraining.com/react-router/core/api/history
So presumably, you have an index.js file that looks like this or should look like this where you added <BrowserRouter> in accordance to the solution:
import { BrowserRouter } from "react-router-dom";
import { Route, Switch } from "react-router";
const App = () => (
<Switch>
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route path="/invoices/:id" component={Invoice} />
</Switch>
);
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
el
);
But, what if your index.js file looked like this instead:
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore, applyMiddleware, compose } from "redux";
import reduxThunk from "redux-thunk";
import App from "./components/App";
import reducers from "./reducers";
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
reducers,
composeEnhancers(applyMiddleware(reduxThunk))
);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.querySelector("#root")
because you created an index.js file that is at the top-level of your component hierarchy and then an App.js file, that looks like this:
import React from "react";
import { Route } from "react-router-dom";
import Dashboard from "./invoices/Dashboard";
import Invoice from "./invoices/Invoice";
import Header from "./Header";
const App = () => {
return (
<div className="ui container">
<div>
<Header />
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route path="/invoices/:id" component={Invoice} />
</div>
</div>
);
};
export default App;
In such a case, you can use the exact keyword on all your Routes and encapsulate it all inside of <BrowserRouter> and ensure you have that encapsulated in <div>s like so:
import React from "react";
import { BrowserRouter, Route } from "react-router-dom";
import Dashboard from "./invoices/Dashboard";
import Invoice from "./invoices/Invoice";
import Header from "./Header";
const App = () => {
return (
<div className="ui container">
<BrowserRouter>
<div>
<Header />
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route exact path="/invoices/:id" component={Invoice} />
</div>
</BrowserRouter>
</div>
);
};
export default App;

The link is being rendered but not redirecting using <Link>

I have been trying to redirect the page using <Link> and what i have is that the URL changes but the page does not redirect. Only after i refresh the page, it show.
I have searched and found some links:
1. One on the correct syntax
2. i have implemented the link in small HTML
Now here is the part of my Code
App.Js
import React from 'react';
import { Switch , Route , BrowserRouter } from 'react-router-dom';
import HomePage from './section/home';
import Oneup from './section/oneup';
function App() {
return (
<div className="main homepage">
<BrowserRouter>
<div>
<Switch>
<Route path="/" component={HomePage} exact={true} />
<Route path="/oneup" component={Oneup} exact={true} />
</Switch>
</div>
</BrowserRouter>
</div>
);
}
main_content.js
Here i have included <Link>
import React from 'react';
import { BrowserRouter, Link } from "react-router-dom";
class Main_content extends Component {
render() {
return (
<div class="ib-center">
<BrowserRouter>
<Link to="/oneup" class="btn">VIEW CASE</Link>
</BrowserRouter>
</div>
)
}
}
Now i can't figure out where i am going wrong.
the link generated is fine and working when refreshed manually.
Use one BrowerRouter to wrap, you have used BrowerRouter in App.js and main_content.js too
class Main_content extends Component {
render() {
return (
<div class="ib-center">
<div>
<Link to="/oneup" class="btn">VIEW CASE</Link>
</div>
</div>
)
}

I use React Router 4 in React page but I can't see my component in Route path

[
import React, { Component } from "react";
import { BrowserRouter as Router , Route } from "react-router-dom";
const NewRout = () => {
return(
<p> MY ROUTE </p>
);
}
class App extends Component {
render() {
return (
<Router>
<Route path="/signin" Component={NewRout} />
</Router>
);
}
}
export default App;
]1I'm using router in my react page. But I can't see output.
I import BrowserRouter and Route from react-route-dom
and try to show mt component inside the route.but this is not working for me.Please help me how to solve this issue. Thanks
<BrowserRouter><Route path="signin" Component={Signin} /></BrowserRouter>
You have a mistake in your path:
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route, Link } from "react-router-dom";
import "./styles.css";
const Home = () => (
<h1>
Home <Link to="/signin">SING</Link>
</h1>
);
const SingIn = () => (
<h1>
<Link to="/">Home</Link>
This is singin page
</h1>
);
ReactDOM.render(
<div>
<BrowserRouter>
<div>
<Route exact path="/" component={Home} />
<Route path="/signin" component={SingIn} />
</div>
</BrowserRouter>
</div>,
document.getElementById("root")
);
Now locate to http://localhost:port/singin you will see your component.
Note: I added a / before your path. This denotes that you are going to signin from your root that is /.
You need to use a prop called exact which matches for exact Route.
Try this SandBox
https://codesandbox.io/s/moj8kxp0nx

Resources