我正在尝试对视图和其他封装函数使用自定义钩子。
但由于某种原因,我需要更新父级的数据。对于这种情况,我将处理程序从父级传递给自定义钩子,并且该处理程序正在访问钩子状态。
/* this is hook */
const customhook = useCustomHook({ init: true });
/* parent is calling hook function to update data */
const actionFromParent = e => {
customhook.UpdateFromParentAction("First Data intialized");
};
/* hook is assigning parent function */
const actionFirst = e => {
customhook.SomeAction({
data: "action first",
init: true,
handler: actionFromParent
});
};
/* inside hook, it is calling parent function */
{state.handler && <button onClick={state.handler}>Click Last</button>}
我在这里附加了沙箱代码: https ://codesandbox.io/s/zen-napier-i6v5g?file=/src/custom.jsx:790-860
翻阅古今
相关分类