Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get withRouter to pass match params to component?

I want to access the match params on the navigation on my app, I'm using react and react-router-dom. Here is a code snipped for simple example I made, as you can see on the Topics component I get the correct match at component level but not in the nav bar, I do get the correct url path in the location prop so I'm not sure if I'm doing this right.

This would be the working snippet, since I couldn't add react-router-dom to the stack overflow snippet.

import { BrowserRouter as Router, Route, Link, withRouter } from "react-router-dom";

const BasicExample = (props) =>
  <Router>
    <div>
      <Nav/>
      <hr />

      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/topics" component={Topics} />
    </div>
  </Router>;


const About = () => (
  <div>
    <h2>About</h2>
  </div>
);

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
);

const Navigation = (props) => (
  <ul>
    <li>
      <Link to="/">Home</Link>
    </li>
    <li>
      <Link to="/about">About</Link>
    </li>
    <li>
      <Link to="/topics">Topics</Link>
    </li>
    <li>{`match prop -> ${JSON.stringify(props.match)}`}</li>
    <li>{`location prop -> ${JSON.stringify(props.location)}`}</li>
  </ul>
);

const Nav =  withRouter(Navigation);

const Topic = ({ match, location }) => (
  <div>
    <h3>{match.params.topicId}</h3>
    <li>{`match prop -> ${JSON.stringify(match)}`}</li>
    <li>{`location prop -> ${JSON.stringify(location)}`}</li>
  </div>
);

const Topics = ({ match, location, history }) => (
  <div>
    <h2>Topics</h2>
    <li>{`match prop -> ${JSON.stringify(match)}`}</li>
    <li>{`location prop -> ${JSON.stringify(location)}`}</li>
    <ul>
      <li>
        <Link to={`${match.url}/rendering`}>
          Rendering with React
        </Link>
      </li>
      <li>
        <Link to={`${match.url}/components`}>
          Components
        </Link>
      </li>
      <li>
        <Link to={`${match.url}/props-v-state`}>
          Props v. State
        </Link>
      </li>
    </ul>

    <Route path={`${match.url}/:topicId`} component={Topic} />
    <Route
      exact
      path={match.url}
      render={() => <h3>Please select a topic.</h3>}
    />
  </div>
);


ReactDOM.render(<BasicExample />, document.getElementById("root"));
<body>
  <div id="root"></div>
  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
</body>
like image 923
piptin Avatar asked Aug 17 '17 12:08

piptin


People also ask

Is withRouter outdated?

The library-provided HOC, withRouter, has been deprecated in React Router v6. If you need to use v6 and are using class-based React components, then you will need to write your own HOC which wraps the v6 use* hooks.

What can I use instead of withRouter?

You can import the useNavigate hook and issue a relative navigation from the current location. Example: import React from "react"; import './menuItem. style.

Why withRouter is not working?

The error "export 'withRouter' (imported as 'withRouter') was not found in 'react-router-dom'" occurs because the withRouter function has been removed in react router v6. To solve the error, install version 5.2. 0 of react router by running npm install [email protected] .


2 Answers

For detecting :topicId in <Nav> use matchPath imported from react-router:

import { matchPath } from 'react-router'

matchPath(location.pathname, { 
  path:'/topics/:topicId',
  exact: true,
  strict: false
}})
like image 109
Melounek Avatar answered Sep 21 '22 18:09

Melounek


This is a little outdated now. Can be achieved with react-router-dom by simply doing something like:

import { withRouter } from 'react-router-dom';

console.log(props.match.params);

Don't forget to wrap the component inside withRouter

export default withRouter(YourComponent);

props.match.params will return an object with all the params.

like image 45
Devchris Avatar answered Sep 24 '22 18:09

Devchris