【问题标题】:React Router: Call component function on RouteHandler refReact Router:在 RouteHandler ref 上调用组件函数
【发布时间】:2015-07-22 17:42:20
【问题描述】:

我正在尝试在子组件 RouteHandler 上调用组件函数。

var Bar = React.createClass({
  baz: function() {
    console.log('something');
  },
  render: function() {
    return <div />;
  },
});

var Foo = React.createClass({
  baz: function() {
    this.refs['REF'].baz();
  },
  render: function() {
    return <RouteHandler ref="REF" />;
  },
);

其中RouteHandler 是Bar,但this.refs['REF'].baz 是undefined。

有关组件功能的更多信息,请参阅https://facebook.github.io/react/tips/expose-component-functions.html。

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    我不相信 react-router 目前支持在 RouteHandlers 上公开组件功能,当前的 hacky 解决方法是:

    this.refs['REF'].refs['__routeHandler__'].baz();
    

    见https://github.com/rackt/react-router/issues/1597

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      相关资源
      最近更新 更多