Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export useIsomorphicLayoutEffect + docs #451

Merged
merged 2 commits into from
Jul 10, 2019
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,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/>
Expand Down
24 changes: 24 additions & 0 deletions docs/useIsomorphicLayoutEffect.md
Original file line number Diff line number Diff line change
@@ -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);
```
7 changes: 7 additions & 0 deletions src/__stories__/useIsomorphicLayoutEffect.story.tsx
Original file line number Diff line number Diff line change
@@ -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')} />
));
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -101,6 +102,7 @@ export {
useHover,
useHoverDirty,
useIdle,
useIsomorphicLayoutEffect,
useKey,
useKeyboardJs,
useKeyPress,
Expand Down
4 changes: 0 additions & 4 deletions src/useIsomorphicLayoutEffect.ts
Original file line number Diff line number Diff line change
@@ -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;