Languages
[Edit]
EN

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

0 points
Created by:
Dirask Community
6900

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.

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