React change component inside de main div based on Sidebar link - reactjs

I`m beginner in React and I want to create page where I include a Navbar and Sidebar and the main to by changed by Sidebar link. How I do that?
exemple:
import Sidebar from '../Sidebar';
import Navbar from '../Navbar;
import {Page1,Page2,Page3} from './menu'
export default function UserPage(){
return(
<>
<Navbar />
<Sidebar />
<div>
//Here i want to component be loaded based on Navlink
</div>
</>
)
}
I found the problem!
If you keep the navbar or sidebar in path="/" don`t use exact in the route because when you will go to "/dashboard" he will not add them

Use a Router
For routing in react you can look up react router here
You then define your routes and that will fill your content based on url.
Example
See how this is like a normal react component, each route will load some specified components. Anything that exists on every page can be put outside of the Switch component. You cna see the Nav bar here that will exist on every page.
export default function App() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/users">Users</Link>
</li>
</ul>
</nav>
{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/users">
<Users />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}

Related

implement nested routing in React js react-router-dom V6 [duplicate]

This question already has an answer here:
Nested routing in react-router-dom V6
(1 answer)
Closed 1 year ago.
I have a folder structure in my project like this :
---login
---main
in the App I implement routing like this that works fine for me :
import { BrowserRouter as Router, Routes, Route} from 'react-router-dom';
<div className="App">
<Router>
<Routes>
<Route path="/" element={<Login />} />
<Route path="/main" element={<Main />} />
</Routes>
</Router>
</div>
but my problem starts here . When I go to the main page, I want to render the nested routing inside of the main page . I code like that but unfortunately nothing work for me fine.
here is the code in my main component .
import {
BrowserRouter as Router,
Routes,
Route,
Outlet,
Link
} from 'react-router-dom';
<
<h1>
hello Home
</h1>
<Link to="/main/child1">
child 1
</Link>||||
<Link to="/main/child2">
child 2
</Link>
<Link to="/">
log out
</Link>
<Outlet />
<Routes>
<Route path="/main/child1" element={<Children1 />} />
<Route path="/main/child2" element={<Children2 />} />
</Routes>
I want to render the child at the under of my main component but nothing happen. and want to url change to "/main/child1" .
I would be really appreciate it if you can explain the detail .
Thanks.
here is the sample code sample is here
You can use the following code:
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
useRouteMatch,
useParams
} from "react-router-dom";
export default function App() {
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Login</Link>
</li>
<li>
<Link to="/main">Main</Link>
</li>
</ul>
<Switch>
<Route path="/">
<Login />
</Route>
<Route path="/main">
<Main />
</Route>
</Switch>
</div>
</Router>
);
}
function Login() {
return <h2>LoginPage</h2>;
}
function Main() {
let match = useRouteMatch();
return (
<div>
<h2>Main Page</h2>
<ul>
<li>
<Link to={`${match.url}/children`}>children</Link>
</li>
<li>
<Link to={`${match.url}/child1`}>
Child_One
</Link>
</li>
</ul>
<Switch>
<Route path={`${match.path}/:childId`}>
<Child />
</Route>
<Route path={match.path}>
<h3>Please select a child.</h3>
</Route>
</Switch>
</div>
);
}
function Child() {
let { childId } = useParams();
return <h3>Requested child ID: {childId}</h3>;
}
The Main page has its own with more routes that build on the /main URL path. You can think of the 2nd here as an "index" page for all children of the main page, or the page that is shown when no child is selected.
For more info, please refer: link

Regarding React router dom

Hope you are doing good. I have one question regarding react routing. I have 5 components defined in app.js file after one by one and created route for that as shown in app.js file and created header for that as well as shown in header.js file. But the problem is this when I should click on the 4th component defined in header then focus should come near to 4th component, instead of displaying 4th component below 5th component. for reference I'm giving one website link I want to make routing like this website, if you didn't understand my question.
Refernce Website link
https://www.styleshout.com/templates/preview/Ceevee_2_0_0/
Note: I have completed this issue, for solution please check this link
https://github.com/pajjwal1/Resume
***App.jS***
import './App.css';
import { Route, Switch} from 'react-router-dom'
import Header from './Components/Header/header';
import Home from './Components/Home/home'
import Bg from './Asset/header-bg.jpg'
import About from './Components/About/about'
import Technical from './Components/Technical/technical'
import Project from './Components/Project/project'
import Education from './Components/Education/education'
import Personal from './Components/Personal/personal'
function App() {
return (
<div className="App">
<Header />
<Home background = {Bg}/>
<About />
<Technical />
<Project />
<Education />
<Personal />
<Switch>
<Route path='/home' exact component={Home}></Route>
<Route path='/about' component={About}></Route>
<Route path='/technical' component={Technical}></Route>
<Route path='/project' component={Project}></Route>
<Route path='/education' component={Education}></Route>
<Route path='/personal' component={Personal}></Route>
</Switch>
</div>
);
}
export default App;
***header.js***
import React from 'react';
import '../Header/header.css'
import {NavLink} from 'react-router-dom'
function header(){
return (
<div className="header">
<div className="header_center">
{/* <p>Home</p>
<p>About</p>
<p>Technical Expertiese</p>
<p>Projects</p>
<p>Qualification</p>
<p>Personal Details</p> */}
<NavLink className='nav-item' to='/home'>Home</NavLink>
<NavLink className='nav-item' to='/about'>About</NavLink>
<NavLink className='nav-item' to='/technical'>Technical Expertiese</NavLink>
<NavLink className='nav-item' to='/project'>Projects</NavLink>
<NavLink className='nav-item' to='/education'>Qualification</NavLink>
<NavLink className='nav-item' to='/personal'>Personal Details</NavLink>
</div>
</div>
);
}
export default header;
I see what you want. Do you want to scroll to the component when you click on the nav item of the header?
In this case, you should use a hash router.
Please put id to each component that you want to navigate on the page.
And use # for hash navigate.
For example, if you want to navigate to projects component, use like this.
<Link to="/#projects"> Projects </Link>
And have you wrapped your app component with Router?
import { createBrowserHistory as history } from 'history'
...
<Router history={history()}>
...
<App />
...
</Router>
I hope this helps you to solve your problem.
If it doesn't work for you, please contact me.
I'm sure I can solve this kind of problem.
I will list the things that I updated on your project.
#app.js
Remove Switch wrapper with children
...
{/* <Switch>
<Route path='/home' exact component={Home}></Route>
<Route path='/about' component={About}></Route>
<Route path='/technical' component={Technical}></Route>
<Route path='/#project' component={Project}></Route>
<Route path='/#education' component={Education}></Route>
<Route path='/personal' component={Personal}></Route>
</Switch> */}
...
#header.js
Please use a tag instead of Navlink
<a className='nav-item' href='/#home'>Home</a>
<a className='nav-item' href='/#about'>About</a>
<a className='nav-item' href='/#technical'>Technical Expertiese</a>
<a className='nav-item' href='/#project'>Projects</a>
<a className='nav-item' href='/#education'>Qualification</a>
<a className='nav-item' href='/#personal'>Personal Details</a>
...
And you should put id on every component as like as the project component.
If it doesn't work, please let me know or invite me to your GitHub repo.
Thanks.
Are you going to scroll down to the component when you click the navigation item on header?
For that you can use hash router.
For more detail about that, you can reference this site.
https://paulgrajewski.medium.com/using-context-and-hashrouter-in-react-87afcefc5966
Is it helpful for you?

how to use router inside a routed component

My App.js:
<Router>
<Header/>
<Switch>
<Route exact path="/" component={HomeScreen} />
<Route exact path="/screenOne" component={OneScreen} />
<Route exact path="/screenTwo" component={TwoScreen} />
</Switch>
</Router>
The <Header /> has three links to the respective components viz. HomeScreen, OneScreen, TwoScreen.
I want my <TwoScreen /> to be exactly like this baseComponent(i.e App.js) where I have some links and when I click those links, the components corresponding to the link/path gets rendered.
What is the best way to approach this?
there is an example on the react-router site that meets your need, under nesting, you can check it out https://reactrouter.com/web/example/nesting (for just web)
But for me, I simply declare them all in one place/component, especially on react-router-native, if you're doing react-native.
below is the example from their website for web
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
useParams,
useRouteMatch
} from "react-router-dom";
// Since routes are regular React components, they
// may be rendered anywhere in the app, including in
// child elements.
//
// This helps when it's time to code-split your app
// into multiple bundles because code-splitting a
// React Router app is the same as code-splitting
// any other React app.
export default function NestingExample() {
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
<hr />
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/topics">
<Topics />
</Route>
</Switch>
</div>
</Router>
);
}
function Home() {
return (
<div>
<h2>Home</h2>
</div>
);
}
function Topics() {
// The `path` lets us build <Route> paths that are
// relative to the parent route, while the `url` lets
// us build relative links.
let { path, url } = useRouteMatch();
return (
<div>
<h2>Topics</h2>
<ul>
<li>
<Link to={`${url}/rendering`}>Rendering with React</Link>
</li>
<li>
<Link to={`${url}/components`}>Components</Link>
</li>
<li>
<Link to={`${url}/props-v-state`}>Props v. State</Link>
</li>
</ul>
<Switch>
<Route exact path={path}>
<h3>Please select a topic.</h3>
</Route>
<Route path={`${path}/:topicId`}>
<Topic />
</Route>
</Switch>
</div>
);
}
function Topic() {
// The <Route> that rendered this component has a
// path of `/topics/:topicId`. The `:topicId` portion
// of the URL indicates a placeholder that we can
// get from `useParams()`.
let { topicId } = useParams();
return (
<div>
<h3>{topicId}</h3>
</div>
);
}

React Nested Routing by `:id`

I am trying to create a blog style app with an "articles" page that has a list of posts which will render by postId. I have gotten the urls to generate and even my small <Post /> component to render, but when a post page renders the Articles content doesn't go away and the post content just renders below it. How do I fix this?
I have
App.js:
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route exact path="/articles">
<Articles />
</Route>
</Switch>
Articles.js:
import React from 'react';
import {
Link,
useRouteMatch,
Switch,
Route,
} from 'react-router-dom';
import Post from '../Components/Post';
import Banner from './Banner';
const Articles = () => {
let match = useRouteMatch();
return (
<div className='Articles'>
<Banner title='Articles'/>
<h3>Please select a topic.</h3>
<ul>
<li>
<Link to={`${match.url}/geojsons`}>Geojsons</Link>
</li>
<li>
<Link to={`${match.url}/props-v-state`}>
Props v. State
</Link>
</li>
</ul>
<Switch>
<Route path={`${match.url}/:postId`}>
<Post />
</Route>
</Switch>
</div>
)
}
export default Articles;
Post.js:
function Post() {
let { postId } = useParams();
return <h3>POST - Requested topic ID: {postId}</h3>;
}
export default Post;
Screen shot of problem page:
This screenshot shows the page after you click on an article link. The Post content is what I have in the dashed red box and everything above it should only be a part of the Articles page.
I know it is only a couple files but to make it easier to mess with I put a simplified version of the repo on github. No styling or anything just an html version of the problem.
Since you want Article and Posts pages to be separate, you need to decouple your post route from Article route and render it in App.js instead of Articles.js
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/articles/:postId">
<Post />
</Route>
<Route exact path="/articles">
<Articles />
</Route>
</Switch>
and in Article.js
const Articles = () => {
let match = useRouteMatch();
return (
<div className='Articles'>
<Banner title='Articles'/>
<h3>Please select a topic.</h3>
<ul>
<li>
<Link to={`${match.url}/geojsons`}>Geojsons</Link>
</li>
<li>
<Link to={`${match.url}/props-v-state`}>
Props v. State
</Link>
</li>
</ul>
</div>
)
}

How to link to a nested route in React?

I have an AngularJS background, and started to play around with React. I'm using the react-router and want have basically this setup:
Template
export default ( props ) => {
return (
<div>
<Navbar></Navbar>
{ props.children }
</div>
);
}
Routing in App.js
<Router>
<Route path="/" component={Template}>
<IndexRoute component={RestaurantRoulette} />
<Route name="workspace" path="workspace(/:workspace_id)" component={RestaurantRoulette}>
<Route name="run" path="run" component={Run}></Route>
</Route>
</Route>
</Router>
Navbar
<ul className="navigation">
<li>RestaurantRoulette</li>
<Link to="run">Run</Link>
<li>Save</li>
</ul>
What I want
When I am in localhost:8080/workspaces/444 I want to click on the Run Link, and navigate to localhost:8080/workspaces/444/run
What is the current status
If I manually type the url localhost:8080/workspaces/444/run, everything works fine. But when I click on the Link in the navbar, I get to localhost:8080/run.
Can somebody tell me how this works? How this is all connected?
I think the Link tag should be like that:
<Link to={'workspaces/' + this.props.workspace_id + '/run'}>Run</link>
you should get the workspace_id from path in Template, and pass it to the NavBar component

Resources