Since react-onclickoutside uses a Higher-Order Component (HOC) pattern, the component you wrap is not directly accessible via a standard React ref. To access the original component's instance and its API (methods, state, etc.), use the getInstance() method provided by the HOC on the ref object.
Note: There is also a getClass() function available to retrieve the original Class, but it is recommended to access the instance for runtime operations.
import React, { Component } from 'react'
import onClickOutside from 'react-onclickoutside'
class MyComponent extends Component {
// ...
customFunction() {
console.log('Called!');
}
}
var EnhancedComponent = onClickOutside(MyComponent);
class Container extends Component {
constructor(props) {
super(props);
this.getMyComponentRef = this.getMyComponentRef.bind(this);
}
someFunction() {
var ref = this.myComponentRef;
// 1) Get the wrapped component instance:
var superTrueMyComponent = ref.getInstance();
// and call instance functions defined for it:
superTrueMyComponent.customFunction();
}
getMyComponentRef(ref) {
this.myComponentRef = ref;
}
render() {
return <EnhancedComponent disableOnClickOutside={true} ref={this.getMyComponentRef}/>
}
}