Skip to content

Commit c00f308

Browse files
committed
feat: 🎸 add useUpdate hook
1 parent bfc30b9 commit c00f308

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
- [`useSpring`](./docs/useSpring.md) — interpolates number over time according to spring dynamics.
5555
- [`useTimeout`](./docs/useTimeout.md) — returns true after a timeout.
5656
- [`useTween`](./docs/useTween.md) — re-renders component, while tweening a number from 0 to 1. [![][img-demo]](https://codesandbox.io/s/52990wwzyl)
57+
- [`useUpdate`](./docs/useUpdate.md) — returns a callback, which re-renders component when called.
5758
<br/>
5859
<br/>
5960
- [**Side-effects**](./docs/Side-effects.md)

docs/useUpdate.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# `useUpdate`
2+
3+
React utility hook that returns a function that forces component
4+
to re-render when called.
5+
6+
7+
## Usage
8+
9+
```jsx
10+
import {useUpdate} from 'react-use';
11+
12+
const Demo = () => {
13+
const update = useUpdate();
14+
return (
15+
<>
16+
<div>Time: {Date.now()}</div>
17+
<button onClick={update}>Update</button>
18+
</>
19+
);
20+
};
21+
```

src/__stories__/useUpdate.story.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {storiesOf} from '@storybook/react';
2+
import * as React from 'react';
3+
import {useUpdate} from '..';
4+
import ShowDocs from '../util/ShowDocs';
5+
6+
const Demo = () => {
7+
const update = useUpdate();
8+
return (
9+
<>
10+
<div>Time: {Date.now()}</div>
11+
<button onClick={update}>Update</button>
12+
</>
13+
);
14+
};
15+
16+
storiesOf('useUpdate', module)
17+
.add('Docs', () => <ShowDocs md={require('../../docs/useUpdate.md')} />)
18+
.add('Demo', () =>
19+
<Demo/>
20+
)
21+

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import useTitle from './useTitle';
3131
import useToggle from './useToggle';
3232
import useTween from './useTween';
3333
import useUnmount from './useUnmount';
34+
import useUpdate from './useUpdate';
3435
import useWindowSize from './useWindowSize';
3536

3637
export {
@@ -67,5 +68,6 @@ export {
6768
useToggle,
6869
useTween,
6970
useUnmount,
71+
useUpdate,
7072
useWindowSize,
7173
};

src/useUpdate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {useState} from './react';
2+
3+
const useUpdate = () => useState(0)[1] as (() => void);
4+
5+
export default useUpdate;

0 commit comments

Comments
 (0)