feat: expose useIsomorphicLayoutEffect + docs (#451)

Export useIsomorphicLayoutEffect + docs
This commit is contained in:
Vadim Dalecky 2019-07-10 15:04:39 +02:00 committed by GitHub
commit 8dcbbf16cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 4 deletions

View File

@ -110,6 +110,7 @@
- [`useMount`](./docs/useMount.md) — calls `mount` callbacks.
- [`useUnmount`](./docs/useUnmount.md) — calls `unmount` callbacks.
- [`useUpdateEffect`](./docs/useUpdateEffect.md) — run an `effect` only on updates.
- [`useIsomorphicLayoutEffect`](./docs/useIsomorphicLayoutEffect.md) — `useLayoutEffect` that does not show warning when server-side rendering.
- [`useDeepCompareEffect`](./docs/useDeepCompareEffect.md) — run an `effect` depending on deep comparison of its dependencies
<br/>
<br/>

View File

@ -0,0 +1,24 @@
# `useIsomorphicLayoutEffect`
`useLayoutEffect` that does not show warning when server-side rendering, see [Alex Reardon's article](https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a) for more info.
## Usage
```jsx
import {useIsomorphicLayoutEffect} from 'react-use';
const Demo = ({value}) => {
useIsomorphicLayoutEffect(() => {
window.console.log(value)
}, [value]);
return null;
};
```
## Reference
```ts
useIsomorphicLayoutEffect(effect: EffectCallback, deps?: ReadonlyArray<any> | undefined);
```

View File

@ -0,0 +1,7 @@
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import ShowDocs from './util/ShowDocs';
storiesOf('Lifecycle|useIsomorphicLayoutEffect', module).add('Docs', () => (
<ShowDocs md={require('../../docs/useIsomorphicLayoutEffect.md')} />
));

View File

@ -25,6 +25,7 @@ import useGetSetState from './useGetSetState';
import useHover from './useHover';
import useHoverDirty from './useHoverDirty';
import useIdle from './useIdle';
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
import useKey from './useKey';
import useKeyboardJs from './useKeyboardJs';
import useKeyPress from './useKeyPress';
@ -102,6 +103,7 @@ export {
useHover,
useHoverDirty,
useIdle,
useIsomorphicLayoutEffect,
useKey,
useKeyboardJs,
useKeyPress,

View File

@ -1,9 +1,5 @@
import { useEffect, useLayoutEffect } from 'react';
/**
* `useLayoutEffect` that does not show warning on server.
* See: https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a
*/
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
export default useIsomorphicLayoutEffect;