- Ensure that each custom hook is concurrent-mode safe. This requires following certain principles, such as:
- avoiding reading or writing refs during the render process.
- When using external state management libraries, useSyncExternalStore must be used. However, this usually doesn't concern us as we generally don't store setState.
- Given that stable event handler hooks would only have incorrect values when read within a child component's useLayoutEffect, I believe we can temporarily ignore this scenario and uniformly wrap translated event handlers with useEvent.
function Example(props) {
const callback = useDynamicCallback(props.callback);
return <Child callback={callback} />;
}
function Child({ callback }) {
useLayoutEffect(() => {
// Child effects are run before parent effects!
// This means the callback ref has not yet been updated-
// which means that it still points to the previous version,
// with stale values and potentially different behavior.
// This function call will probably cause problems!
callback();
}, []);
}
references:
facebook/react#16956 (comment)
facebook/react#18003 (comment)
references:
facebook/react#16956 (comment)
facebook/react#18003 (comment)