It sort of looks hard without breaking the rule
const Component = ({ outer }) => {
const [mutable, setMutable] = useState(0);
const [cond, setCond] = useState(false);
useEffect(() => {
const handle = doExpensiveSubscription((value) => {
if (cond) {
// cond and outer are stale values
setMutable(calculate(outer, value));
}
});
return handle.unsubscribe;
// the ESLint exhaustive-deps rule would warn about unmet dependency: outer, cond
}, []);
return <div onClick={() => setCond(!cond)}>{mutable}</div>;
};
My temporary solution to the problem looks like
const Component = ({ outer }) => {
const [mutable, setMutable] = useState(0);
const [cond, setCond] = useState(false);
const paramRef = useRef({});
paramRef.current = { outer, cond };
useEffect(() => {
const handle = doExpensiveSubscription((value) => {
const { outer, cond } = paramRef.current;
if (cond) {
// cond and outer hold their respective current values now
setMutable(calculate(outer, value));
}
});
return handle.unsubscribe;
// the ESLint exhaustive-deps rule would warn about unmet dependency: outer, cond
}, []);
return <div onClick={() => setCond(!cond)}>{mutable}</div>;
};
There does not seem to be a specific documentation about that.
It sort of looks hard without breaking the rule
My temporary solution to the problem looks like
There does not seem to be a specific documentation about that.