-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Fast Refresh/Live Reload interaction with React hooks (#7952)
Closes #5870
- Loading branch information
1 parent
8d41184
commit d83d207
Showing
4 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
/** | ||
* This hook allows running a function only immediately after a react | ||
* fast refresh or live reload. | ||
* | ||
* Useful in order to ensure that we can reinitialize things that have been | ||
* disposed by cleanup functions in `useEffect`. | ||
* @param effectFn a function to run immediately after a fast refresh | ||
*/ | ||
export function useAfterFastRefresh(effectFn: () => unknown) { | ||
if (__DEV__) { | ||
const didRefresh = useRef(false); | ||
useEffect(() => { | ||
return () => { | ||
// Detect fast refresh, only runs multiple times in fast refresh | ||
didRefresh.current = true; | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (didRefresh.current === true) { | ||
// This block only runs after a fast refresh | ||
didRefresh.current = false; | ||
effectFn(); | ||
} | ||
}, []) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters