Languages
[Edit]
EN

React / react-router - pass props to a child component using Router

0 points
Created by:
christa
600

In this article, we would like to show you how to pass props to a child component using React Router.

Below we create MyComponent that receives props via Route element's render() method.

Then, we simply use props.data to access the props.

Practical example:

import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';

const MyComponent = (props) => (
  <h2>
    {props.data.map((fruit) => (
      <li key={fruit}>{fruit},</li>
    ))}
  </h2>
);

const App = () => {
  const fruits = ['🍏 Apple', '🍌 Banana', '🍒 Cherry'];

  return (
    <Router>
      <Route path="/" render={() => <MyComponent data={fruits} />} />
    </Router>
  );
};

export default App;

You can run this example here.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

React - react-router

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join