我有3个功能组件,我想传递&通过useRef
操作数据。
我将onClose
方法添加到我的组件之一的current
属性中。
const onClose = () => {
setButtonColor(buttonColor);
};
ref.current = {
clearSwitchStateOnClose: onClose,
};
我正在另一个组件中调用该方法。
ref.current.clearSwitchStateOnClose();
我收到这个错误,
Uncaught TypeError: ref.current.clearSwitchStateOnClose is not a function
我调用这个方法ref.current.clearSwitchStateOnClose();
,但我也变异了ref
。我想这就是问题的根源。
大概是这样的,
ref.current = {
showModal: false,
modalResponse: null,
};
ref.current.clearSwitchStateOnClose();
执行此操作后,它工作正常。
ref.current.clearSwitchStateOnClose();
ref.current = {
showModal: false,
modalResponse: null,
};
很抱歉没有分享这个情况的全部情况。