-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(View): add useLayoutEffectCall (#7164)
- see #6920 --- > This mutates a variable that React considers immutable Компилятор ругается на мутацию afterTransition * refactor: call to add
- Loading branch information
1 parent
e1dcb63
commit cdf2646
Showing
2 changed files
with
51 additions
and
13 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,42 @@ | ||
import * as React from 'react'; | ||
import { useIsomorphicLayoutEffect } from '../../lib/useIsomorphicLayoutEffect'; | ||
|
||
class LayoutEffectCall { | ||
#fns: Array<() => void> = []; | ||
|
||
/** | ||
* Выполняет переданные функции | ||
*/ | ||
run() { | ||
for (const fn of this.#fns) { | ||
fn(); | ||
} | ||
|
||
this.#fns = []; | ||
} | ||
|
||
/** | ||
* Вызовет функцию после изменения DOM, но до того как пользователь увидит | ||
* изменения | ||
*/ | ||
add = (fn: () => void) => { | ||
this.#fns.push(fn); | ||
}; | ||
} | ||
|
||
/** | ||
* Возвращает функцию которая вызывает callback после изменения DOM, но до того | ||
* как пользователь увидит изменения | ||
*/ | ||
export function useLayoutEffectCall() { | ||
const ref = React.useRef<LayoutEffectCall | null>(null); | ||
if (!ref.current) { | ||
ref.current = new LayoutEffectCall(); | ||
} | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
ref.current!.run(); | ||
}); | ||
|
||
return ref.current.add; | ||
} |