EN
React / react-router - pass props to a child component using Router
0
points
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.