-
Notifications
You must be signed in to change notification settings - Fork 94
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
Deprecate useSafeState #1096
Deprecate useSafeState #1096
Conversation
useSafeState is an anti-pattern (detailed more in the associated PR) which merely hides a React's warning. As of React 18, this warning is not even shown anymore, which makes this hook even more unnecessary. BREAKING CHANGE: useSafeState is removed
Codecov Report
@@ Coverage Diff @@
## master #1096 +/- ##
==========================================
- Coverage 98.52% 98.49% -0.03%
==========================================
Files 63 62 -1
Lines 1082 1064 -18
Branches 180 179 -1
==========================================
- Hits 1066 1048 -18
Misses 2 2
Partials 14 14
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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 for thorough explanation.
We will let it to marinate a bit more to avoid overwhelming with breaking changes (supposedly we should have a new rule - one breaking change in a month 😅)
What do you think about also removing |
That one still have valid usecases, when dealing with external queues, high frequency events etc. |
Hello @xobotyi , it's been a while. Do you think it would be time to merge this? |
yup |
🎉 This PR is included in version 23.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What is the current behavior?
useSafeState
is offered as a solution to situations where asynchronous functions attempt to update a component's state when the component has already unmounted, resulting in the famousCan't perform a React state update on an unmounted component
error.useSafeSate
is likeuseState
, but it does anisMounted
check before allowing the state to be set. This, according to the React team, is an anti-pattern. A more fresh take on the topic, confirmed by Dan Abramov, can be found here. I won't go in to the details of whyisMounted
checks are an anti-pattern, because I don't want to repeat the resources linked above.Additionally, the
Can't perform a React state update on an unmounted component
error has been removed in React 18, because it was considered to be misleading and caused people to invent workarounds, which the React team considered harmful (the example of a harmful workaround in the linked discussion is actually almost exactlyuseIsMounted
).For the above reasons, I think
useSafeState
has no job anymore. Lastly, it should also be considered, whether or notuseIsMounted
should be removed also.This issue was also discussed a while back in #959 .
What is the expected behavior?
useState
should be used instead ofuseSafeState
. Asynchronous functions should be cancelled if the component unmounts or they should be ignored, if cancellation is not an option. From the author of the discussion linked above:How does this PR fix the problem?
useSafeState
withuseState
.useSafeState
.useSafeState
from the documentation and examples.Checklist