Skip to content

Commit 88483bd

Browse files
rootroot
authored andcommitted
[FIX] replace useStateGetter with useStateGetReset
1 parent ac724e7 commit 88483bd

7 files changed

Lines changed: 42 additions & 35 deletions

File tree

apps/react-tools-demo/src/components/hooks/useStateGetter/InputMemo.tsx renamed to apps/react-tools-demo/src/components/hooks/useStateGetReset/InputMemo.tsx

File renamed without changes.

apps/react-tools-demo/src/components/hooks/useStateGetter/UseStateGetter.tsx renamed to apps/react-tools-demo/src/components/hooks/useStateGetReset/UseStateGetReset.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { BaseSyntheticEvent, useCallback, useState } from 'react';
2-
import { useStateGetter } from '../../../../../../packages/react-tools/src/hooks/useStateGetter';
2+
import { useStateGetReset } from '../../../../../../packages/react-tools/src/hooks/useStateGetReset';
33
import { Input } from './InputMemo';
44

55
/**
66
The component has:
7-
- A _useStateGetter_ that receives an object, with three properties _id_, _name_, _eta_, and returns a tuple with _stateG_, _setterG_ and _getter_.
7+
- A _useStateGetReset_ that receives an object, with three properties _id_, _name_, _eta_, and returns a tuple with _stateG_, _setterG_, _getter_ and _resetter_.
88
- A _useState_ that receives same object of useStateGetter and returns a tuple with _state_ and _setter_.
99
- Each property of _state_ and _stateG_ is rendered an Input component that renders the input and label fields and the _component's number of renders_.
1010
- A _onChangeGetter_ function made with useCallback to handle stateGetter input onChange, reading other values with getter function.
1111
- A _onChange_ function made with useCallback to handle stater input onChange, reading other values by state variable.
12+
- A button that executes the _resetter_ function on _stateG_.
1213
The two functions onChange and onChange Getter update their respective _state_ every time they are executed. Since the _onChange_ function depends on the state, every time this changes it will be reevaluated and this will also trigger the rerender of the input components that have not undergone a change to their _value_ variable. The _onChangeGetter_ doesn't have this behavior: since the _getter_ function isn't reevaluated even if the _stateG_ changes, so the _onChangeGetter_ is never reevaluated and only the input component that has a change in the _value_ variable is rerendered.
1314
*/
14-
const UseStateGetter = () => {
15-
const [stateG, setStateG, getState] = useStateGetter({ id: "", name: "", eta: "" });
15+
const UseStateGetReset = () => {
16+
const [stateG, setStateG, getState, resetState] = useStateGetReset({ id: "", name: "", eta: "" });
1617
const [state, setState] = useState({ id: "", name: "", eta:"" });
1718

1819
const onChangeGetter = useCallback((e: BaseSyntheticEvent) => {
@@ -33,15 +34,16 @@ const UseStateGetter = () => {
3334
return (
3435
<div style={{ display: "grid", gridTemplateColumns: "auto auto", justifyContent: "center", gap: 50 }}>
3536
<div>
36-
<p><strong>With getter</strong></p>
37-
<div style={{ gridTemplateRows: "auto auto auto", justifyContent: 'center', display: 'grid', gap: '10px' }}>
37+
<p><strong>With get and reset</strong></p>
38+
<div style={{ gridTemplateRows: "auto auto auto auto", justifyContent: 'center', display: 'grid', gap: '10px' }}>
3839
<Input id="idG" name="id" value={stateG.id} onChange={onChangeGetter} />
3940
<Input id="nameG" name="name" value={stateG.name} onChange={onChangeGetter} />
4041
<Input id="etaG" name="eta" value={stateG.eta} onChange={onChangeGetter} />
42+
<button onClick={resetState}>Reset State</button>
4143
</div>
4244
</div>
4345
<div>
44-
<p><strong>Without getter</strong></p>
46+
<p><strong>Without get and reset</strong></p>
4547
<div style={{ gridTemplateRows: "auto auto auto", justifyContent: 'center', display: 'grid', gap: '10px' }}>
4648
<Input id="id" name="id" value={state.id} onChange={onChange} />
4749
<Input id="name" name="name" value={state.name} onChange={onChange} />
@@ -52,6 +54,6 @@ const UseStateGetter = () => {
5254
);
5355
};
5456

55-
UseStateGetter.displayName = "UseStateGetter";
57+
UseStateGetReset.displayName = "UseStateGetReset";
5658

57-
export { UseStateGetter };
59+
export { UseStateGetReset };

apps/react-tools-demo/src/constants/components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const COMPONENTS = [
22
[
33
"usePrevious",
4-
"useStateGetter",
4+
"useStateGetReset",
55
"useStateHistory",
66
"useStateHistoryGetter",
77
"useMemoCompare",

apps/react-tools-demo/src/markdown/UseStateGetter.md renamed to apps/react-tools-demo/src/markdown/UseStateGetReset.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# useStateGetter
2-
Custom useState with getter state function.
1+
# useStateGetReset
2+
Custom useState with get and reset state functions.
33

44
## Usage
55

66
```tsx
7-
const UseStateGetter = () => {
8-
const [stateG, setStateG, getState] = useStateGetter({ id: "", name: "", eta: "" });
7+
const UseStateGetReset = () => {
8+
const [stateG, setStateG, getState, resetState] = useStateGetReset({ id: "", name: "", eta: "" });
99
const [state, setState] = useState({ id: "", name: "", eta:"" });
1010

1111
const onChangeGetter = useCallback((e: BaseSyntheticEvent) => {
@@ -26,15 +26,16 @@ const UseStateGetter = () => {
2626
return (
2727
<div style={{ display: "grid", gridTemplateColumns: "auto auto", justifyContent: "center", gap: 50 }}>
2828
<div>
29-
<p><strong>With getter</strong></p>
30-
<div style={{ gridTemplateRows: "auto auto auto", justifyContent: 'center', display: 'grid', gap: '10px' }}>
29+
<p><strong>With get and reset</strong></p>
30+
<div style={{ gridTemplateRows: "auto auto auto auto", justifyContent: 'center', display: 'grid', gap: '10px' }}>
3131
<Input id="idG" name="id" value={stateG.id} onChange={onChangeGetter} />
3232
<Input id="nameG" name="name" value={stateG.name} onChange={onChangeGetter} />
3333
<Input id="etaG" name="eta" value={stateG.eta} onChange={onChangeGetter} />
34+
<button onClick={resetState}>Reset State</button>
3435
</div>
3536
</div>
3637
<div>
37-
<p><strong>Without getter</strong></p>
38+
<p><strong>Without get and reset</strong></p>
3839
<div style={{ gridTemplateRows: "auto auto auto", justifyContent: 'center', display: 'grid', gap: '10px' }}>
3940
<Input id="id" name="id" value={state.id} onChange={onChange} />
4041
<Input id="name" name="name" value={state.name} onChange={onChange} />
@@ -45,24 +46,25 @@ const UseStateGetter = () => {
4546
);
4647
};
4748

48-
UseStateGetter.displayName = "UseStateGetter";
49+
UseStateGetReset.displayName = "UseStateGetReset";
4950

50-
export { UseStateGetter };
51+
export { UseStateGetReset };
5152
```
5253

5354
> The component has:
54-
> - A _useStateGetter_ that receives an object, with three properties _id_, _name_, _eta_, and returns a tuple with _stateG_, _setterG_ and _getter_.
55+
> - A _useStateGetReset_ that receives an object, with three properties _id_, _name_, _eta_, and returns a tuple with _stateG_, _setterG_, _getter_ and _resetter_.
5556
> - A _useState_ that receives same object of useStateGetter and returns a tuple with _state_ and _setter_.
5657
> - Each property of _state_ and _stateG_ is rendered an Input component that renders the input and label fields and the _component's number of renders_.
5758
> - A _onChangeGetter_ function made with useCallback to handle stateGetter input onChange, reading other values with getter function.
5859
> - A _onChange_ function made with useCallback to handle stater input onChange, reading other values by state variable.
60+
> - A button that executes the _resetter_ function on _stateG_.
5961
> The two functions onChange and onChange Getter update their respective _state_ every time they are executed. Since the _onChange_ function depends on the state, every time this changes it will be reevaluated and this will also trigger the rerender of the input components that have not undergone a change to their _value_ variable. The _onChangeGetter_ doesn't have this behavior: since the _getter_ function isn't reevaluated even if the _stateG_ changes, so the _onChangeGetter_ is never reevaluated and only the input component that has a change in the _value_ variable is rerendered.
6062
6163

6264
## API
6365

6466
```tsx
65-
useStateGetter <T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T]
67+
useStateGetReset <T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T, () => void]
6668
```
6769

6870
> ### Params
@@ -73,5 +75,5 @@ value or a function.
7375
7476
> ### Returns
7577
>
76-
> - __array__: _[T, Dispatch<SetStateAction<T>>, () => T]_
78+
> - __array__: _[T, Dispatch<SetStateAction<T>>, () => T, ()=>void]_
7779
>

apps/react-tools-demo/src/router/Router.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ComponentLayout from '../layout/ComponentLayout';
44
import MarkdownLayout from '../layout/MarkdownLayout';
55
import HomeWrapper from '../components/home/HomeWrapper';
66
import usePreviousMD from '../markdown/UsePrevious.md?url';
7-
import useStateGetterMD from '../markdown/UseStateGetter.md?url';
7+
import useStateGetResetMD from '../markdown/UseStateGetReset.md?url';
88
import useStateHistoryMD from '../markdown/UseStateHistory.md?url';
99
import useStateHistoryGetterMD from '../markdown/useStateHistoryGetter.md?url';
1010
import useCallbackCompareMD from '../markdown/UseCallbackCompare.md?url';
@@ -27,7 +27,7 @@ import { UseMemoCompare } from '../components/hooks/useMemoCompare/UseMemoCompar
2727
import { UseMemoDeepCompare } from '../components/hooks/useMemoDeepCompare/UseMemoDeepCompare';
2828
import { UseEffectCompare } from '../components/hooks/useEffectCompare/UseEffectCompare';
2929
import { UseEffectDeepCompare } from '../components/hooks/useEffectDeepCompare/UseEffectDeepCompare';
30-
import { UseStateGetter } from '../components/hooks/useStateGetter/UseStateGetter';
30+
import { UseStateGetReset } from '../components/hooks/useStateGetReset/UseStateGetReset';
3131

3232
function Router() {
3333
const router = createBrowserRouter([
@@ -47,10 +47,10 @@ function Router() {
4747
/>
4848
},
4949
{
50-
path: "/useStateGetter",
50+
path: "/useStateGetReset",
5151
element: <ComponentLayout
52-
component={<UseStateGetter />}
53-
markdown={useStateGetterMD}
52+
component={<UseStateGetReset />}
53+
markdown={useStateGetResetMD}
5454
/>
5555
},
5656
{

packages/react-tools/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# TODO
22
- __STATE__
33
- [x] usePrevious
4-
- [x] useStateGetter
5-
- [ ] useStateRestore
4+
- [x] useStateGetReset
65
- [x] useStateHistory
76
- [x] useStateHistoryGetter
7+
- [ ] useReducerGetReset
88
- [ ] useReducerHistory
9-
- [ ] useReducerGetter
10-
- [ ] useReducerRestore
119
- [ ] useLocalStorage
1210
- [ ] useSessionStorage
1311
- [ ] useMap
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from "react"
22

33
/**
4-
* **`useStateGetter`**: custom useState with getter state function.
4+
* **`useStateGetReset`**: custom useState with get and reset state functions.
55
* @param {T | () => T} initialState - value or a function.
6-
* @returns {[T, Dispatch<SetStateAction<T>>, () => T]} array
6+
* @returns {[T, Dispatch<SetStateAction<T>>, () => T, ()=>void]} array
77
*/
8-
export const useStateGetter = <T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T] => {
8+
export const useStateGetReset = <T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T, () => void] => {
99
const [state, setState] = useState<T>(initialState);
1010
const getterRef = useRef<() => T>(() => state);
11+
1112
const getter = useCallback(() => getterRef.current(), []);
13+
14+
const resetter = useCallback(() => setState(initialState), [initialState]);
15+
1216
useEffect(() => {
1317
getterRef.current = () => state
1418
});
1519

1620
return [
1721
state,
1822
setState,
19-
getter
23+
getter,
24+
resetter
2025
];
2126
}

0 commit comments

Comments
 (0)