H
H
Hellas2017-02-08 11:25:41
React
Hellas, 2017-02-08 11:25:41

How to pass data from component to component using routes?

class Header extends Component {
  constructor(props) {
    super(props);
    this.state = {
      select: false
    };
  }
  render() {
    return <div>{this.state.select}</div>;
  }
}

class Main extends Component {
  send = () => {
    // Отсюда необходимо изменить состояние в Header на this.state.select = true
  }
  render() {
    return <button onClick={this.send}></div>;
  }
}

ReactDOM.render(
  <Router history={browserHistory}>
    <Route path="/" component={Header}>
      <Route path="main" component={Main}/>
    </Route>
  </Router>, 
  document.querySelector('.app')
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2017-02-08
@Hellas

Here is a general approach to this issue.
But it is applicable in academic projects or tiny applications.
In reality, you need to use dataflow packages. Please love and favor - Redux .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question