M
M
mamaanarhiya2018-02-19 23:40:13
JavaScript
mamaanarhiya, 2018-02-19 23:40:13

How to transfer data via NavLink?

How can I pass data to another component via NavLink?
Is there something like this?

<NavLink to="/test" state={myData}>ссылка</NavLink>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2018-02-20
@mamaanarhiya

If you use Link instead of NavLink, then you can
https://reacttraining.com/react-router/web/api/Link
something like this

<Link to={{
  pathname: '/test',
  state: { data: myData }
}}/>

and then get this.props.location.state.data on the test page

K
kn1ght_t, 2018-02-19
@kn1ght_t

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

<Router>
  <div>
    <NavLink to="/test">ссылка</NavLink>
    <Route path="/test" render={props => <SomeComponent {...props} state={myData} />} />
  </div>
</Router>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question