0

could you please tell me how to send data from one component to another component in react ? I do like this

 <Route path="/second/:testvalue" component={Second}/>

on Link function

 <Link to="/second/"{this.state.username}><button onClick={this.handleClick}>send data</button></Link>

But it not passing my data

here is my code http://codepen.io/anon/pen/mOYgJV?editors=1011

Sergey Kovalev
  • 9,110
  • 2
  • 28
  • 32
user5711656
  • 3,310
  • 4
  • 33
  • 70

3 Answers3

0

React is build for Flux pattern. You emit events, than a dispatcher though PubSub pattern passes those event to subscribers, which are usually components.

Take a look at official description at https://facebook.github.io/flux/.

Sergey Kovalev
  • 9,110
  • 2
  • 28
  • 32
0

Here you can use props to pass data between two components

<Second data={your data }/>

in First Component you can access the data using props by writing this.props.data.

in your example you can set the state of data on button click and you can pass the state value in <Second data={this.state.data }/>

0

Try this:

<Link to={`/second/${this.state.username}`}><button onClick={this.handleClick}>send data</button></Link>
Guangning Yu
  • 31
  • 2
  • 7