I would like to call a method from a child component, as per suggested here Call child method from parent
However, it doesn't work when the child component is wrapped with connect from react-redux as in the example below:
Child Component
interface OwnProps {
style?: any;
}
interface ReduxStateProps {
category: string;
}
interface DispatchProps {
updateTimestamp: (timestamp: Date) => void;
}
type Props = ReduxStateProps & DispatchProps & OwnProps;
interface State {
timestamp?: Date;
}
class ChildComponent extends React.Component<Props, State> {
childMethod = () => {
console.log("I am the child");
};
render(){
<Text>Debug</Text>
}
}
function mapStateToProps(state: any): ReduxStateProps {
return {
category: state.menu.category
};
}
function mapDispatchToProps(dispatch: Dispatch<any>): DispatchProps {
return {
updateTimestamp: (timestamp: Date) =>
dispatch(updateTimestampActionCreator(timestamp))
};
}
export default connect<ReduxStateProps, DispatchProps, OwnProps>(
mapStateToProps,
mapDispatchToProps
)(ChildComponent);
Parent Component
class ParentComponent extends React.Component<{},{}> {
private childComponent: any;
render(){
<View>
<ChildComponent ref={ref => this.childComponent = ref as any}>
</View>
}
}
Here, the method "childMethod" is undefined when using the ref in the parent component. Without using connect, everything works fine.