5

Original question: https://github.com/acdlite/recompose/issues/232

How can I access the ref of the BaseComponent or any level of the components?

Code:

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" />
   }
}
Fernando Montoya
  • 2,625
  • 1
  • 18
  • 21

1 Answers1

3

You can use hoistStatic.

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

const enhance = compose(...some)

const Component = hoistStatics(enhance)(BaseComponent)

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

You can find a real example in the test.

wuct
  • 10,057
  • 2
  • 19
  • 22