【发布时间】:2021-07-11 09:10:40
【问题描述】:
我想在父组件中调用子组件的函数。这是我的伪代码:
export const Child: React.FC<Props> = ({ onRef }) => {
useEffect(() => {
if (onRef) {
onRef(this);
}
}, [])
function callMe() {
console.log('Child has been called');
}
return <Text>Child</Text>;
}
export const Parent: React.FC<Props> = () => {
const childRef = useRef(null);
function callChild() {
childRef.current.callMe();
}
return <Child onRef={childRef} />;
}
我该怎么做?
【问题讨论】:
-
看看
useImperativeHandlereactjs.org/docs/hooks-reference.html#useimperativehandle -
@konstantin 谢谢!
标签: reactjs react-native ref