我有一个父组件,我需要调用其子组件的 2 个方法。我可以使用 useImperativeHandler 来调用一个像
const Parent = () => {
const childRef = useRef();
return (
<div>
<Child ref={childRef} />
<Button onClick={() => childRef.current.methodOne()}>
Submit
</Button>
</div>
);
};
然后在子组件中
const Child = forwardRef((props, ref) => {
useImperativeHandle(
ref,
() => ({
methodOne() {
// some code
},
}),
[]
);
return;
});
到目前为止,效果非常好。但我希望父组件中的另一个按钮可以调用同一个子组件中的第二个方法(我们称之为 methodTwo)。我该怎么做?
慕盖茨4494581
慕哥9229398
相关分类