-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove ref in render antipattern in animated docs #4200
Conversation
✅ Deploy Preview for react-native ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
✅ Deploy Preview for react-native ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Isn't useAnimatedValue also reading ref.current in render? https://github.com/facebook/react-native/blob/8b2ccea243d8b8156d6f6e1e3e08b6064f82ed84/packages/react-native/Libraries/Animated/useAnimatedValue.js#L24 |
5527857
to
fe4ace8
Compare
Yeah that's true. But updating the docs means that we can remove a Rule of React violation from user code, so if React Compiler is being used these components/hooks can still be compiled, while the offending useAnimatedValue will not (if we compile it). |
67733f9
to
5856434
Compare
5856434
to
0e3d1c9
Compare
Reading or writing to a ref in render is a [rule of React violation](https://react.dev/reference/react/useRef). This PR updates the docs for animated to use the `useAnimatedValue` hook instead, which does use a ref under the hood but isolates the rule of React violation to just that hook. This allows users that follow code examples for animated to have their components and hooks be compilable by React Compiler. This hook was added in [0.71](https://github.com/facebook/react-native/blob/main/CHANGELOG.md#:~:text=Introduce%20useAnimatedValue%20hook%20to%20make%20it%20easier%20working%20with%20Animated.Values%20in%20function%20components.%20(e22217fe8b%20by%20%40fabriziocucci)) so I'm also updating versioned docs from 0.71 onwards.
0e3d1c9
to
f6728c4
Compare
I missed a few examples in #4200 where the example expo sandboxes were broken due to useAnimatedValue not being exported on web.
I missed a few examples in #4200 where the example expo sandboxes were broken due to useAnimatedValue not being exported on web.
Reading or writing to a ref in render is a rule of React violation. This PR updates the docs for animated to use the
useAnimatedValue
hook instead, which does use a ref under the hood but isolates the rule of React violation to just that hook.This allows users that follow code examples for animated to have their
components and hooks be compilable by React Compiler.
This hook was added in
0.71
so I'm also updating versioned docs from 0.71 onwards.