简体   繁体   English

反应:传递ref通过?

[英]React: pass ref through?

I'm trying to figure out how to get access to a "public" method on a React Component, that's been decorated with any number of higher-order components. 我试图弄清楚如何访问React组件上的“公共”方法,该方法已经装饰有任意数量的高阶组件。 Something like this: 像这样:

class Toolbar extends React.Component {
  openSubMenu(menuName) {
    // whatever
  }

  render() {
    // whatever
  }
}

export default compose(
  connect(mapStateToProps, mapDispatchToProps),
  preload(), // custom higher order component which displays loading state
)(Toolbar);

If I now get a ref on the toolbar, the openSubmenu method isn't available, because it's hidden under (in this case 2) higher order components. 如果现在在工具栏上获得ref ,则openSubmenu方法不可用,因为它隐藏在(在本例中为2个)高级组件下。 Is there anyway to get to an arbitrary method on the underlying component? 无论如何,在底层组件上可以使用任意方法吗?

Refs Aren't Passed Through While the convention for higher-order components is to pass through all props to the wrapped component, it's not possible to pass through refs. 引用未通过尽管高阶组件的约定是将所有prop传递到包装的组件,但无法通过ref。 That's because ref is not really a prop — like key, it's handled specially by React. 这是因为ref并不是真正的道具-像键一样,它是由React专门处理的。 If you add a ref to an element whose component is the result of an HOC, the ref refers to an instance of the outermost container component, not the wrapped component. 如果将ref添加到其元素是HOC结果的元素,则ref引用最外层容器组件的实例,而不是包装的组件。

check https://facebook.github.io/react/docs/higher-order-components.html 检查https://facebook.github.io/react/docs/higher-order-components.html

Check the working demo: JSFiddle 检查工作演示: JSFiddle

Based on the one-way data flow nature of React, a component function is always triggered by some passed-in props value: 基于React one-way data flow特性,组件函数总是由一些传入的prop值触发的:

var Toolbar = React.createClass({
  render: function() {
    console.log(this.props);
    return <div className={this.props.active ? "active" : ""}>Menu Item</div>;
  }
});

var Parent = React.createClass({
  open: function() {
    this.setState({
      subMenuActive: true
    });
  },
  render: function() {
    console.log(this.state);
    var isActive = (this.state || {})['subMenuActive'];
    return <div><Toolbar active={isActive}></Toolbar><button onClick={this.open}>Open sub menu</button></div>;
  }
});

So the process should be reversed , which means: a component should be used by others, which will trigger different behaviours by specifying different props value. 因此,该过程应该相反 ,这意味着:组件应由其他组件使用,这将通过指定不同的props值来触发不同的行为。

If you want to trigger some specific function inside the component (instead of changing a className in the example), you need to write some code in some special functions like componentWillReceiveProps or componentDidMount . 如果要触发组件内部的某些特定功能(而不是更改示例中的className),则需要在一些特殊功能(例如componentWillReceivePropscomponentDidMount )中编写一些代码。 There functions will be called by React at some particular moment: https://facebook.github.io/react/docs/component-specs.html React将在某个特定时刻调用这些函数: https : //facebook.github.io/react/docs/component-specs.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM