React router doesn't load page from other component page

Sahil Mohile

I am using react with django in backend. When the link is clicked from component page I want the respective page to open. But page doesn't render anything.

This is my homepage.js where main react router is defined.

 <Router>
        <Navigation />
        <Switch>
            <Route exact path= "/">
                <Home />
            </Route>
            <Route path="/communication">
                <Communication />
            </Route>
            {/* <Route path="/forum-create" component={ForumCreate} /> */}
            <Route path="/calendar">
                <EventCalendar />
            </Route>
            <Route path="/information">
                <Informations />
            </Route>
            <Route path="/links">
                <Links />
            </Route>
        </Switch>
    </Router>

I have defined the Route in another from which I want to render page.

import React from 'react'
import {BrowserRouter as Router, Switch, Route, Link, Redirect,} from 'react-router-dom'


function Communication() {
    return (
        <div>
            <Forum />
            {/* <Route path="/forum-create" component={ForumCreate} /> */}

        </div>
    )
}


function Forum() {
    return (
        <div>
            <h2 className="page-head">Forum</h2>
            <div className="forum">
                <Link to="forum-create">Create Topic</Link>
                <table className="forum-table">
                    <th>
                        <td>Topic</td>
                    </th>
                    <th>
                        <td>Date</td>
                    </th>
                    <th>
                        <td>Posted By</td>
                    </th>
                    <tr>
                        <td>Test Topic</td>
                        <td>20/05/2021</td>
                        <td>Awesome User</td>
                    </tr>
                    <tr>
                        <td>Test Topic</td>
                        <td>20/05/2021</td>
                        <td>Awesome User</td>
                    </tr>
                </table>
            </div>
        </div>
    )
}


function ForumCreate() {
    return (
        <div>
            Create a Topic Forum
            Some Chagnes
        </div>
    )
}

export default Communication

I want to render component ForumCreate() from communication page only. I have tried putting path in homepage.js inside Route but still clicking on Link doesn't load up the ForumCreate function. I can render this function if its in homepage.js

Drew Reese

When Communication component is being rendered the current path is path="/communication", so if you now render additional routes that have a different path prefix (i.e. something other than "/communication") like path="/forum-create", then when you link to that route the Switch no longer matches "/communication" and the Communication component unmounts, thus unmounting the <Route path="/forum-create" component={ForumCreate} /> route component.

The solution is to either declare the forum create route out at the root level with the Switch so it's always available to be matched and rendered, or nested as a sub-route of "/communication".

As a root level route.

<Router>
  <Navigation />
  <Switch>
    <Route exact path= "/">
      <Home />
    </Route>
    <Route path="/communication">
      <Communication />
    </Route>
    <Route path="/forum-create" component={ForumCreate} /> // <-- render in root
    <Route path="/calendar">
      <EventCalendar />
    </Route>
    <Route path="/information">
      <Informations />
    </Route>
    <Route path="/links">
      <Links />
    </Route>
  </Switch>
</Router>

Render as a nested route.

function Communication() {
  return (
    <div>
        <Forum />
        <Route
          path="/communication/forum-create" // <-- nested route
          component={ForumCreate}
        />
    </div>
  )
}

function Forum() {
  return (
    <div>
      <h2 className="page-head">Forum</h2>
      <div className="forum">
        <Link to="/communication/forum-create"> // <-- nested route
          Create Topic
        </Link>
        ...
      </div>
    </div>
  )
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Nested react router component doesn't load/render on page reload?

React Router doesn't load the new page component after redirecting via navigate

React router redirects the page but component doesn't get rendered

React Router change the address but doesn't load the second page

page doesn't load on refresh - react-router-dom

React router - page doesn't change when navigating from outside component

React doesn't get id when fetching from other page

React Router 6 doesn't redirect to page

React router doesn't refresh page

React page doesn't load the new content

Link in react router dom doesn't load page only url browser nav gets changed

how to navigate from one page to other page using react-router-dom-v6 in class-based component?

React Router loads URL in address bar but doesn't load component

React - Dynamically load a component into the page

My Link tag from react-router doesn't change my page

React Router changes URL but doesn't update the page unless it was refresh from browser

React Router page address change but doesn't refresh

React router force to another page if a variable doesn't exist

react-router-dom Link doesn't go to page content

Navigate in react router 6 doesn't redirect to another page

Next page doesn't load

VueJS Router doesn't load the component

React Router render component on a separate page

React router render component on same page

Problem rendering Component in a different page with React Router

trigger an automatic download from javascript doesn't work on page load

htaccess remove extension from url works but the page doesn't load

How to navigate to other page using react router

React Router trigger re-load / re-mount component on redirecting to same page with different slug