React-router-dom routing to a new page with props

Bentaiba Miled Basma

My question might seem stupid because I don't have enough background in React JS. I have this component:

import { BrowserRouter, Route, NavLink } from "react-router-dom";
import CourseInfo from "./CourseInfo";
import OneCourse from "./OneCourse";

class CourseList extends Component {
  render() {
    return (
      <div>
        <BrowserRouter>
          <div className="row courses">
            {this.props.corses.map(course => (
              <NavLink key={course._id} to={`/courses/profile/${course._id}`}>
                <OneCourse course={course} />
              </NavLink>
            ))}
          </div>
          <Route
            exact
            path={`/courses/profile/:id`}
            render={({ match }) => (
              <CourseInfo
                index={match.params.id}
                course={
                  this.props.corses.filter(el => el._id === match.params.id)[0]
                }
              />
            )}
          />
        </BrowserRouter>
      </div>
    );
  }
}

When I click on OneCourse component, it shows me the CourseList component in the same page with the component CourseInfo added in the bottom.

How can I send user to a new page containing only CourseInfo, knowing that I have parameters to send from this component to CourseInfo? I want to show CourseInfo component in a different page that doesn't contain the CourseList

Dennis Cieplik

<NavLink> is just fine. For the rest, you might use the <Switch> component:

import { BrowserRouter, Route, NavLink, Switch } from 'react-router-dom';
class CourseList extends Component {
  render() {
    return (
      <div>
        <BrowserRouter>
          <Switch>
          <Route
            exact
            path={`/`} render={({ match }) => (
              <div className="row courses">
                {this.props.corses.map(course => (
              <NavLink key={course._id} to={`/courses/profile/${course._id}`}>
                <OneCourse course={course} />
              </NavLink>
            ))}
            </div>
            )}
          />
          <Route
            exact
            path={`/courses/profile/:id`}
            render={({ match }) => (
              <CourseInfo
                index={match.params.id}
                course={
                  this.props.corses.filter(el => el._id === match.params.id)[0]
                }
              />
            )}
          />
          </Switch>
        </BrowserRouter>
      </div>
    );
  }
}

Quoting from the docs:

Switch is unique in that it renders a route exclusively. In contrast, every that matches the location renders inclusively.

With complex routing the render props approach gets confusing. Moving the routing to a separate component is a better approach:

import React from 'react';
import { BrowserRouter, Route, Link, Switch } from "react-router-dom";
import CourseList from './CourseList';
import Course from './Course';

function App() {
  return (
    <BrowserRouter>
      <Switch>
       <Route exact path="/" component={CourseList} />
       <Route exact path="/courses/profile/:id" component={Course} />
       </Switch>
    </BrowserRouter>
  );
}

export default App;

Then your CourseInfo component looks like:

class CourseList extends Component {   
  render() {
    return (
      <div>

          <div className="row courses">
            {this.props.corses.map(course => (
              <NavLink key={course._id} to={`/courses/profile/${course._id}`}>
                <OneCourse course={course} />
              </NavLink>
            ))}
          </div>
      </div>
    );   
} }

The official documentation provides plenty examples.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Problem with multiple page routing in react-router-dom

React/React-Router: componentWillUnmount not called when routing to new page

pass props to new page via react-router Link

react-router-dom rendering new page over current page

React router dom dynamic routing

How to create a dynamic routing naming the routes based on props of a component in react-router-dom

How to navgiate to a new page using react router dom with typescript

Scroll to a specific div on a new page using react router dom

Routing to another page React Router

React - React Router Dom, hashbang routing solution

Pass id as props in react-router-dom

react-router-dom Conditional Routing on Submit?

react routing with parcel and router dom 6.3

React Router DOM not rendering the page

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

How to pass extra props to new page through <Link> with React-Router?

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

React router appends page at last when routing

Cannot pass props to Router in react-router-dom

Position of a new page with react router

How to setup dynamic routing with React Router based on props received?

React react-router-dom pass props to component

Routing problem in React-JS / react-router-dom

React Routing with Express, Webpack dev middleware, React router dom

React router dom v5 history.push does not render new page

React-router-dom navigate not rendering page

React-Router-Dom changing states of the page

React-Router-Dom: The content of a page is not displayed

react-router-dom link to another page