Skip to content

Commit d337dd3

Browse files
author
scottrippey
committed
feature(useEvent): added docs
1 parent fdb0322 commit d337dd3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,37 @@
11
# react-useevent
22
Same as React's `useCallback`, but returns a stable reference.
3+
4+
This library is a user-land implementation of the `useEvent` hook, [proposed in this RFC](https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md).
5+
6+
# Installation
7+
8+
```sh
9+
npm install react-useevent
10+
```
11+
12+
# Usage
13+
(this example was copied from the RFC)
14+
15+
You can wrap any event handler into `useEvent`.
16+
17+
```js
18+
import useEvent from 'react-useevent';
19+
20+
function Chat() {
21+
const [text, setText] = useState('');
22+
23+
const onClick = useEvent(() => {
24+
sendMessage(text);
25+
});
26+
27+
return <SendButton onClick={onClick} />;
28+
}
29+
```
30+
31+
The code inside `useEvent` “sees” the props/state values at the time of the call.
32+
The returned function has a stable identity even if the props/state it references change.
33+
There is no dependency array.
34+
35+
# See more
36+
- [The proposed `useEvent` RFC](https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md)
37+
- [A hearty discussion on the naming, and edge-case considerations, of this hook](https://github.com/reactjs/rfcs/pull/220)

0 commit comments

Comments
 (0)