piptin
piptin

Reputation: 365

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>

Upvotes: 5

Views: 7152

Answers (2)

Devchris
Devchris

Reputation: 412

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.

Upvotes: 1

Melounek
Melounek

Reputation: 910

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
}})

Upvotes: 5

Related Questions