Skip to content

Commit 22d4bdf

Browse files
committed
Initial commit of unreleased react-use-value-change
1 parent d768c06 commit 22d4bdf

19 files changed

+35492
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Check:
44
- [typescript-async-timeouts](typescript-async-timeouts/README.md)
55
- [typescript-blocking-queue](typescript-blocking-queue/README.md)
66
- [typescript-nullsafe](typescript-nullsafe/README.md)
7+
- [react-use-value-change](react-use-value-change/README.md)

react-use-value-change/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/dist

react-use-value-change/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Margus Rebane
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

react-use-value-change/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Install
2+
3+
```
4+
$ npm install react-use-value-change --save
5+
```
6+
or
7+
```
8+
$ yarn add react-use-value-change
9+
```
10+
11+
[Direct link to npmjs.com](https://www.npmjs.com/package/react-use-value-change)
12+
13+
# The problem
14+
15+
Quite often I find myself having complex useEffect to detect the state changes. This library helps to keep up state tracking.
16+
17+
It contains Three simplified methods:
18+
19+
- `useStringValueChange`
20+
- `useNumberValueChange`
21+
- `useBooleanValueChange`
22+
23+
## Simple setup
24+
25+
```typescript
26+
const [currentValue, {setValue, resetValue, resetToCurrentValue, resetToOriginalValue}, {equals, original}] = useStringValueChange("the initial value");
27+
```
28+
- `currentValue` is the string current value
29+
- `original` is immutable original value (until value is reset)
30+
- `equals` indicates if `original === currentValue`
31+
- `setValue("new value")` will change the `currentValue`, keeps `original` and `equals` is false if the new value is different form original
32+
- `resetValue()` will reset `original` to `currentValue` and `equals` to true
33+
- `resetValue("something else")` will reset `currentValue` and `original` to "something else" and `equals` to true
34+
- `resetToCurrentValue()` will reset the `original` to `currentValue` and `equals` to true
35+
- `resetToOriginalValue()` will reset the `currentValue` to `original` and `equals` to true
36+
37+
All the other methods follows the same pattern.
38+
39+
## Using react input
40+
41+
The difference are with input listeners
42+
43+
- `useStringInputValueChange` accepts `HTMLTextAreaElement | HTMLInputElement`
44+
- `useNumberInputValueChange` accepts `HTMLInputElement (input type number)`
45+
- `useBooleanInputValueChange` accepts `HTMLInputElement (checkbox or radio)`
46+
47+
Those accept `HTMLTextAreaElement | HTMLInputElement` as setters. Which means one has to call
48+
49+
`setValue(value: HTMLInputElement)` instead of `setValue(value: string)` (or boolean or number)
50+
51+
## a bit more...
52+
53+
There is also an umbrella method for more complex logic: `useValueChange`, which has to be approached carefully as it might trigger recursive rendering.
54+
55+
See [useValueChange.test.ts](https://github.com/dustinest/typescript-async-utils/tree/main/react-usevalue/src/lib/useValueChange.test.ts) how to use it.
56+
57+
58+
## Might be a bug, but it is not
59+
60+
When is used the initial value is immutable. One has to use the React `useffect` to reset the value.

0 commit comments

Comments
 (0)