I am using "connect" on the child component to interact with redux store(am using redux-toolkit). After importing the child component in parent component, I have created reference to the child component using createRef to access child component members.
Child component -->
class ChildComp extends React.Component<IReduxProps, IState>
{
}
default export connect(mapToState, mapToDispatch,null, { forwardRef: true })(ChildComp)
In parent component class
import ChildComp from './childCompFile'
class ParentComp extends React.Component<IReduxProps, IState> {
childRef: React.RefObject<typeof ChildComp>;
constructor(props: {}) {
super(props);
this.childRef= React.createRef<typeof ChildComp>();
}
onChildClick = () => {
this.childRef.current.showChildCompPanel();
};
render() {
<Button onClick={this.onChildClick}></Button>
<ChildComp ref={this.childRef}/>
}
}
Am not able to access any of the methods in child class using this.childRef.current.
Please give an example to show how to access redux connected child class methods using ref from parent class
ChildCompis going to ignore the passedrefprop unless theChildCompcomponent is built to handle it usingforwardRefanduseImperativeHandle. You can do that, but it's probably better to move the show panel state up toParentCompand pass it down through props. This explains the ref approach: stackoverflow.com/a/64491870/10431574