原始问题:https://github.com/acdlite/recompose/issues/232

如何访问BaseComponent或任何级别的组件的引用?

代码:

class BaseComponent {
    doSth(){...}
}
const Component compose(...some)(BaseComponent);

class App {
    componentDidMount() {
        // undefined(doSth) is not a function
        this.refs.component.doSth();
    }
    render() {
        <Component ref="component" />
   }
}

最佳答案

您可以使用hoistStatic

class BaseComponent {
  doSth(){...}
}

const enhance = compose(...some)

const Component = hoistStatics(enhance)(BaseComponent)

class App {
  componentDidMount() {
    this.refs.component.doSth()
  }
  render() {
    <Component ref="component" />
  }
}

您可以找到一个真实的示例in the test

关于reactjs - 如何访问BaseComponent或任何级别的组件的引用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39418581/

10-09 17:29