Skip to content

Commit 730a1ca

Browse files
committed
Fix useLayoutEffect warning
1 parent f6000d8 commit 730a1ca

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.DS_Store
33
node_modules
44
dist
5+
storybook-static

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { spring } from "./spring";
99
import { queueAnimationFrame, unqueueAnimationFrame } from "./raf-queue";
1010
import { currentTime } from "./time";
1111

12+
// https://github.com/facebook/react/issues/14927
13+
const useLayoutEffect =
14+
typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
15+
1216
export function useSpring(
1317
target: number,
1418
config: Config = {}
@@ -26,17 +30,17 @@ export function useSpring(
2630
: spring({ x0, v0, t0, t, k, c, m, X });
2731
const moving = isMoving(x, v, t, newConfig);
2832

29-
React.useLayoutEffect(() => {
33+
useLayoutEffect(() => {
3034
Object.assign(oldConfig, newConfig);
3135
}, [newConfig.X, newConfig.k, newConfig.c, newConfig.m, newConfig.teleport]);
3236

33-
React.useLayoutEffect(() => {
37+
useLayoutEffect(() => {
3438
state.x0 = x;
3539
state.v0 = v;
3640
state.t0 = t;
3741
}, [x, v, t]);
3842

39-
React.useLayoutEffect(() => {
43+
useLayoutEffect(() => {
4044
const loop = (now: number) => {
4145
const { x0, v0, t0 } = state;
4246
const { k, c, m, X, decimals } = oldConfig;
@@ -56,7 +60,7 @@ export function useSpring(
5660
}
5761
});
5862

59-
React.useLayoutEffect(() => {
63+
useLayoutEffect(() => {
6064
return () => {
6165
if (state.raf != null) {
6266
unqueueAnimationFrame(state.raf);

0 commit comments

Comments
 (0)