-
Notifications
You must be signed in to change notification settings - Fork 21
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
Fix issues and react warnings for verification code components #1007
Changes from 5 commits
305c21c
2188f60
49371b5
8f87341
f605132
d9de670
88d40db
65fdefd
14a7fff
3ca2759
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useEffect, useCallback, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Returns a function to check whether the caller component is mounted or unmounted. | ||
* Usually, it's used to avoid the warning - Can't perform a React state update on an unmounted component. | ||
* | ||
* @return {() => boolean} A function returns a boolean indicates the caller component is mounted or unmounted. | ||
eason9487 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export default function useIsMounted() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It had had a similar API
It is difficult to say whether this practice is popular/standard or not because it's not really an issue or bug in most cases when seeing that warning.
The warning has quite a long background, mainly because some earlier global store/state management libraries based on the "Flux Architecture" had subscription and unsubscription mechanisms to be plugged in the class components. As an observer to get the global state changes, if a component forgets to unsubscribe after unmounted, it might let the global store keep the reference of the component itself or the method declared within the component, which may further lead to memory leaks. However, with the implementation of something like react-redux, these subscription processes have been replaced by Higher-Order Components (HOC) and the memory leak problem is no longer common. After react hooks, these warnings are also almost false positives alarm. As it turns out, this warning will be adjusted in the new version of react, and we will no longer need |
||
const mountedRef = useRef( false ); | ||
useEffect( () => { | ||
mountedRef.current = true; | ||
return () => { | ||
mountedRef.current = false; | ||
}; | ||
}, [] ); | ||
return useCallback( () => mountedRef.current, [] ); | ||
} |
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 the detailed explanations :) It makes it clear why the code is here not where it was before.
💅 However, I think we could also add a note of why we need it at all/what it does, for somebody who reads the file as is, not as a change to the previous state.
I made a branch with a small proposal fix/1002-1003-1004-verification-code...fix/1002-1003-1004-verification-code-comments, feel free to merge it if you like it :)
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.
Merged. Thanks!