3
3
<img src="readme-header.svg"/ >
4
4
</a >
5
5
</p >
6
- <h3 align =" center " >Watch <code >ref</code > changes with conditional re-renders</h3 >
6
+ <h3 align =" center " >👀 Watch <code >ref</code > changes with conditional re-renders</h3 >
7
7
<p align =' center ' >(First draft - expect changes) <a href =' https://codesandbox.io/s/ref-change-listener-m5bupr?file=/src/List.js ' >Live demo</a ></p >
8
8
9
9
---
@@ -24,17 +24,21 @@ possibly all the sub-tree as well, which is useless and even harmful for perform
24
24
Each sub-component can register a listener for changes in a specific ` ref ` or any of its ` current ` property's properties
25
25
(assuming ` current ` points to a mutated Object/Array).
26
26
27
- ## Install:
27
+ <br >
28
+
29
+ ## Install with:
28
30
29
31
Use from [ CDN] ( https://unpkg.com/@yaireo/react-ref-watcher ) / Download from this repo / [ NPM] ( https://www.npmjs.com/package/@yaireo/react-ref-watcher )
30
32
31
33
``` bash
32
34
npm i @yaireo/react-ref-watcher -S
33
35
```
34
36
37
+ <br ><br >
38
+
35
39
## What's in this package?
36
40
37
- ### ` useWatchableRef `
41
+ ### 1️⃣ ` useWatchableRef `
38
42
39
43
| Argument | Type | Info
40
44
|---------------|-------|-------------------------------------------------------------
@@ -53,7 +57,9 @@ const Component = () => {
53
57
}
54
58
```
55
59
56
- ### ` useWatchableListener `
60
+ <br >
61
+
62
+ ### 2️⃣ ` useWatchableListener `
57
63
58
64
| Argument | Type | Info
59
65
|----------|----------|-------------------------------------------------------------
@@ -78,8 +84,9 @@ const Component = ({ ref1 }) => {
78
84
}
79
85
```
80
86
87
+ <br >
81
88
82
- ### ` useWatchableEffect `
89
+ ### 3️⃣ ` useWatchableEffect `
83
90
84
91
| Argument | Type | Info
85
92
|---------------|----------|-------------------------------------------------------------
@@ -102,8 +109,9 @@ const Component = ({ ref1, ref2 }) => {
102
109
}
103
110
```
104
111
112
+ <br >
105
113
106
- ### ` propWatcher `
114
+ ### 4️⃣ ` propWatcher `
107
115
108
116
Unlike the other hooks, this is a utility function which does the actual watching.
109
117
It adds an enumerable ` __WATCHERS ` property (will host callback listeners when changes detected)
@@ -112,6 +120,7 @@ on top of the argument (expected to be an *Object*) and then returns a new `prox
112
120
Every time a propery is modified or deleted (in your code) the proxy trap will fire and all
113
121
callback functions defined in the ` __WATCHERS ` property will fire.
114
122
123
+ Any Object may be wrapped with ` propWatcher ` to become "watchable" (not only React refs).
115
124
** Example of most basic usage:**
116
125
117
126
``` js
0 commit comments