generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: should cleanup styles in useEffect cleanup function (#141)
* fix: should cleanup styles in useEffect cleanup function * chore: restore mock * chore: remove only * chore: code pergf * fix: strict mode * chore: update test case
- Loading branch information
Showing
3 changed files
with
192 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { warning } from 'rc-util/lib/warning'; | ||
import * as React from 'react'; | ||
|
||
const fullClone = { | ||
...React, | ||
}; | ||
const { useInsertionEffect } = fullClone; | ||
|
||
// DO NOT register functions in useEffect cleanup function, or functions that registered will never be called. | ||
const useCleanupRegister = () => { | ||
const effectCleanups: (() => void)[] = []; | ||
let cleanupFlag = false; | ||
function register(fn: () => void) { | ||
if (cleanupFlag) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
warning( | ||
false, | ||
'[Ant Design CSS-in-JS] You are registering a cleanup function after unmount, which will not have any effect.', | ||
); | ||
} | ||
return; | ||
} | ||
effectCleanups.push(fn); | ||
} | ||
|
||
React.useEffect(() => { | ||
// Compatible with strict mode | ||
cleanupFlag = false; | ||
return () => { | ||
cleanupFlag = true; | ||
if (effectCleanups.length) { | ||
effectCleanups.forEach((fn) => fn()); | ||
} | ||
}; | ||
}); | ||
|
||
return register; | ||
}; | ||
|
||
const useRun = () => { | ||
return function (fn: () => void) { | ||
fn(); | ||
}; | ||
}; | ||
|
||
// Only enable register in React 18 | ||
const useEffectCleanupRegister = | ||
typeof useInsertionEffect !== 'undefined' ? useCleanupRegister : useRun; | ||
|
||
export default useEffectCleanupRegister; |
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