【问题标题】:How to transfer this.state to react-routes?如何将 this.state 转移到 react-routes?
【发布时间】:2015-10-16 14:12:49
【问题描述】:

我在新的 react-router 中使用 react-router 和 ES6 将值传递给组件?我使用<Handler name = {this.state.name} /> 现在使用{this.props.children}。如何从组件中的 App 向其传输数据?

如何将 this.state 转移到 react-routes 中:

export default class App extends React.Component{

  constructor () {
    this.state = { name: 'Username' };
  }

  render () {
    return (
      <div>
        {this.props.children} // <-- this.state ???
      </div>
    );
  }
};

React.render((
  <Router history={HashHistory}>
    <Route path="" component={App}>
      <Route path="about" component={About} />
    </Route>
  </Router>
), document.getElementById('app'));

{this.props.children} //

【问题讨论】:

    标签: javascript reactjs ecmascript-6 flux react-router


    【解决方案1】:

    我建议从单独的 Flux 存储访问您存储在 App.state 中的信息,然后在您需要的每个组件中访问它们。 React 路由器不应该参与在路由之间传递数据。

    【讨论】:

    • 我完全同意你的观点,但是从主组件授权数据传输更方便。结果,我这样做了:{this.props.children &amp;&amp; React.cloneElement(this.props.children, { username: this.state })}
    • 从 v1.0.0-beta3 的 API 文档中,我找不到您在以前版本中使用的方法,对我来说,这是 API 发展和正确选择的好兆头。
    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2021-04-08
    • 2018-12-23
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多