Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
fix(hooks): Avoid setState in unmounted components. (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBolender authored Mar 4, 2021
1 parent a5ce0a2 commit 32778e8
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ export type ManagerProps = {
export function Manager({ children }: ManagerProps) {
const [referenceNode, setReferenceNode] = React.useState<?Element>(null);

React.useEffect(
() => () => {
setReferenceNode(null);
},
[setReferenceNode]
);
const hasUnmounted = React.useRef(false);
React.useEffect(() => {
return () => {
hasUnmounted.current = true;
};
}, []);

const handleSetReferenceNode = React.useCallback((node) => {
if (!hasUnmounted.current) {
setReferenceNode(node);
}
}, []);

return (
<ManagerReferenceNodeContext.Provider value={referenceNode}>
<ManagerReferenceNodeSetterContext.Provider value={setReferenceNode}>
<ManagerReferenceNodeSetterContext.Provider
value={handleSetReferenceNode}
>
{children}
</ManagerReferenceNodeSetterContext.Provider>
</ManagerReferenceNodeContext.Provider>
Expand Down

0 comments on commit 32778e8

Please sign in to comment.