J
J
Jedi2018-08-22 15:31:51
React
Jedi, 2018-08-22 15:31:51

How to pass props to a component?

Hey!
See, I'm using props. How can I pass the same props to the Menu component?render={props => <Home {...props}/>}

class App extends Component {
  render() {
    return (
      <div className="App">
        <Menu/>
        <main>
          <Switch>
            <Route exact path="/" render={props => <Home {...props}/>}/>
            <Route path="/contacts" render={props => <Contacts {...props}/> }/>
          </Switch>
        </main>
      </div>
    );
  }
}

Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2018-08-22
@PHPjedi

Something like this.

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

const Menu = () => { ... }

export default withRouter(Menu);

Bonus:
In your case, you can just write
<Route exact path="/" component={Home} />
<Route path="/contacts" component={Contacts} />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question