Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/hooks/src/useIsomorphicLayoutEffect/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ To avoid this warning, useIsomorphicLayoutEffect can be used instead of useLayou
The source code of useIsomorphicLayoutEffect is:

```javascript
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : noop;
```

Return useLayoutEffect for browser environment and useEffect for other environments.
Return useLayoutEffect for browser environment and a no-op in non-browser environments to avoid SSR warnings.

For more information, please refer to [useLayoutEffect and SSR](https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a)
5 changes: 3 additions & 2 deletions packages/hooks/src/useIsomorphicLayoutEffect/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useLayoutEffect } from 'react';
import { useLayoutEffect } from 'react';
import isBrowser from '../utils/isBrowser';
import noop from '../utils/noop';

const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : noop;

export default useIsomorphicLayoutEffect;
4 changes: 2 additions & 2 deletions packages/hooks/src/useIsomorphicLayoutEffect/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ nav:
useIsomorphicLayoutEffect 源码如下:

```js
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : noop;
```

在非浏览器环境返回 useEffect,在浏览器环境返回 useLayoutEffect。
在浏览器环境返回 useLayoutEffect,在非浏览器环境返回空函数以避免 SSR 警告

更多信息可以参考 [useLayoutEffect and SSR](https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a)
3 changes: 3 additions & 0 deletions packages/hooks/src/utils/noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const noop = () => {};

export default noop;
Loading