-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Description
I am unsure how to convert some of our code as a result of the new compiler settings.
Say I have a component
function Component () {
let onResize = useEffectEvent(() => {
// ...
});
useEffect(() => {
document.fonts?.ready.then(() => onResize())
});
useResizeObserver(ref, onResize);
}
I'm currently getting an error on the line with the useResizeObserver. However, I know that the useResizeObserver is safe, as its implementation is (simplified):
let useResizeObserver = (ref, onResize) => {
useEffect(() => {
const resizeObserverInstance = new window.ResizeObserver((entries) => {
if (!entries.length) {
return;
}
onResize();
});
resizeObserverInstance.observe(element, {});
return () => {
if (element) {
resizeObserverInstance.unobserve(element);
}
};
}, [ref, onResize])
}
where the onResize is only ever called from something inside the useEffect.
I've added the useResizeObserver hook to the eslint settings as shown in the docs, however, there's nothing that indicates which values are safe or not to be a useEffectEvent. There also doesn't seem to be a way to document this in the types or... anywhere?
https://react.dev/reference/eslint-plugin-react-hooks/lints/rules-of-hooks#options
Also, the lint wants useEffectEvent to be removed from the useEffect dependency array. But that's impossible in a hook that is generalised like this one. You can see I've included it in the dependency array above because I don't know if someone will or already is passing non-useEffectEvents to it.
I feel like I'm not supposed to use this setting here, but I'm not sure what I should be doing instead.
Some other considerations I had:
- Copy the code inside useResizeObserver directly over to my usage, but this will duplicate in many places in our codebase.
- I could have two copies of the function
effectEventone that's a useEffectEvent and one that is a useCallback, but sometimeseffectEventis large and duplicating it would mean that drift might occur in addition to the messy extra copy.