Skip to content
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

Merged
merged 1 commit into from
Oct 30, 2024
Merged

Conversation

poteto
Copy link
Member

@poteto poteto commented Aug 29, 2024

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.

Copy link

netlify bot commented Aug 29, 2024

Deploy Preview for react-native ready!

Name Link
🔨 Latest commit b23f5a5
🔍 Latest deploy log https://app.netlify.com/sites/react-native/deploys/66d08933c9239e000881242e
😎 Deploy Preview https://deploy-preview-4200--react-native.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Aug 29, 2024

Deploy Preview for react-native ready!

Name Link
🔨 Latest commit f6728c4
🔍 Latest deploy log https://app.netlify.com/sites/react-native/deploys/67225c7716c94d00087c26d5
😎 Deploy Preview https://deploy-preview-4200--react-native.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@yungsters yungsters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@gsathya
Copy link

gsathya commented Aug 29, 2024

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

@poteto poteto changed the title Remove antipattern in animated.md Remove ref in render antipattern in animated docs Oct 22, 2024
@poteto
Copy link
Member Author

poteto commented Oct 22, 2024

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

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).

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.
@poteto poteto merged commit 0db3af9 into main Oct 30, 2024
4 checks passed
@poteto poteto deleted the poteto-patch-1 branch October 30, 2024 16:39
poteto added a commit that referenced this pull request Oct 30, 2024
I missed a few examples in #4200 where the example expo sandboxes were broken due to useAnimatedValue not being exported on web.
poteto added a commit that referenced this pull request Oct 30, 2024
I missed a few examples in #4200 where the example expo sandboxes were broken due to useAnimatedValue not being exported on web.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants