react-router-dom v6 rendering new page over current page

Данияр Иркагалиев

the problem is the following. in the app.js is spelled out Routes and the home component. the home contains a navbar that navigates the site, only if you go to any page, it is drawn on top of the home. And if you switch to the home itself, it will be duplicated. Articles on the internet did not help, as did the addition of exact in route path.

function App() {
    return (
        <div className="messenger">

            <Routes>
                <Route path="/home/" element={<Home/>}/>
                <Route path="/settings/" element={<Settings/>}/>
                <Route path="/login/" element={<Login/>}/>
                <Route path="/register/" element={<Register/>}/>
            </Routes>

            <Home/>

        </div>
    )

home

export default class Home extends Component {
    render() {
        return (
            <div>
                <NavBar/>
                <ChatMenu/>
            </div>
        );
    }
}

an example of how it is written in the navbar

    export const NavBar = () => {
            return (<div className="navbar-cm">
        
                    <div className="nav_element">
                        <Link to="/home">
                            <img src={homeIMG} className="nav_element"/>
                        </Link>
                    </div>
and a few more similar ones
    </div>);
    };
Drew Reese

Issue

You are rendering the Home component again once outside the routes, this is why it's rendered with all routes including twice when on the "/home" path that renders Home.

function App() {
  return (
    <div className="messenger">
      <Routes>
        <Route path="/home/" element={<Home />} />
        <Route path="/settings/" element={<Settings />} />
        <Route path="/login/" element={<Login />} />
        <Route path="/register/" element={<Register />} />
      </Routes>

      <Home /> // <-- always rendered below routed content
    </div>
  )
}

Solution

Remove the Home component that is out on its own outside the routes.

function App() {
  return (
    <div className="messenger">
      <Routes>
        <Route path="/home/" element={<Home />} /> // <-- now only Home component rendered
        <Route path="/settings/" element={<Settings />} />
        <Route path="/login/" element={<Login />} />
        <Route path="/register/" element={<Register />} />
      </Routes>
    </div>
  )
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

react-router-dom rendering new page over current page

React Router DOM not rendering the page

React Router V6 - page is not in history

How to redirect to previous page using react-router-dom v6 from a page action() handler?

React-router-dom navigate not rendering page

Function component on the same page in not picking in route (react-router-dom v6) of react project

MUI Drawer not rendering with React-router-dom v6

Reactjs redirect to dashboard page after successful login with react-router-dom (v6)

Javascript function not being called between page navigations and routing using react-router-dom v6

Handle Browser beforeunload with react-router-dom v6 page navigation

React-router-dom v6 didn't show page when try to route

how to redirect to partical page using history object in react-router-dom v6

React Router Dom v6 Detect user leave page and do somthing

React Router Dom v6 - Page Goes Blank after Adding Scroll-to-Top Functionality

React Router v6 - how to handle nested routes but with only current page showing instead of previous pages

How to get current 'page' and pass as param in React Router v6

How can I navgate to the previous page while passing the state of the current page to it using react router v6?

React Router not rendering page

Redirecting in the new React_router-dom v6

React Router v6 <Navigation /> showing blank page

React router v6 experiencing blank page on routes

Page layout broken in React Router v6

React Router V6 Redirection to 404 page

React Router V6 BrowserRouter and Route throws blank page

React-router-dom V6 don't change route correctly (refresh - f5 page is required)

How to redirect to another page with passing data after submitting form in using react-router-dom v6?

react-router-dom Link tag not rendering page

React Router v6 not rendering anything

React Router rendering a blank page