Closed
Description
This isn't really a bug per-se since it's kinda how closures work, but let's say you have a useSpring like this:
const [isOpen, setIsOpen] = React.useState(false);
const [{ xy }, setSpring] = useSpring(() => {
const { x, y } = getDefaultPositions(isOpen, position);
return {
xy: [x, y],
config: animationConfig,
onRest: ya => {
console.log(isOpen);
}
};
});
From my experience, isOpen
will always return false
even if its value has changed to true
within the component. Ideally we could access updated state here.
You can see this in action on this codesandbox. Open the console and try dragging the dot around.
In my experience, you can fix this sort of thing by doing something like this:
function useSpring(fns) {
const callbacks = React.useRef(fns);
React.useEffect(() => {
callbacks.current = fns;
}, [fns])
// and then make calls to callback.current.onRest...
}
I've taken a quick look at the source, but feel a bit like a deer in headlights. Not sure how easy this would be to fix for someone more knowledgeable of the codebase.