better-preact-use-state 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +115 -24
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # better-preact-use-state
1
+ ## better-preact-use-state
2
2
 
3
- A utility that improves Preact's `useState` to be safer and more convenient.
3
+ A utility that improves Preact's `useState` to be safer and more convenient. Provides both `useState` (enhanced) and a dedicated `useBoolState` hook.
4
4
 
5
5
  ## Overview
6
6
 
7
- Preact's `useState` can cause unintended behavior when you want to keep a function as a state. `better-preact-use-state` solves this problem and is a utility designed for safer state management in Preact. It's designed to be a drop-in replacement for Preact's `useState` hook.
7
+ Preact's `useState` can cause unintended behavior when you want to hold a function as a state. `better-preact-use-state` solves this problem and is a utility designed for safer state management. It is designed as a drop-in replacement for Preact's `useState` hook, with added functionality. `better-preact-use-state` also provides a simplified hook, `useBoolState`, specifically for boolean state.
8
8
 
9
9
  ## Installation
10
10
 
@@ -24,29 +24,56 @@ pnpm add better-preact-use-state
24
24
 
25
25
  ## Usage
26
26
 
27
- `better-preact-use-state` can be used the same way as Preact's `useState`.
27
+ ### `useState`
28
+
29
+ `better-preact-use-state`'s `useState` can be used the same way as Preact's `useState`.
28
30
 
29
31
  ```tsx
30
32
  import { useState } from 'better-preact-use-state';
31
33
 
32
34
  const MyComponent: FunctionalComponent = () => {
33
- const [count, setCount] = useState(0);
34
35
  const [name, setName] = useState('John Doe');
35
36
 
36
- const incrementCount = () => setCount(count + 1);
37
- const updateName = (newName: string) => setName(newName);
37
+ const [count, setCount, { updateState: updateCount }] = useState(0);
38
+
39
+ const onNameChange = useCallback((event) => {
40
+ setName(event.target.value);
41
+ }, []);
42
+
43
+ const incrementCount = useCallback(() => {
44
+ updateCount((x) => x + 1);
45
+ }, []);
46
+
47
+ return (
48
+ <div>
49
+ <p>{`Count: ${count}`}</p>
50
+ <button onClick={incrementCount}>{'Increment'}</button>
51
+
52
+ <p>{`Name: ${name}`}</p>
53
+ <input type='text' value={name} onChange={onNameChange} />
54
+ </div>
55
+ );
56
+ };
57
+ ```
58
+
59
+ ### `useBoolState`
60
+
61
+ `useBoolState` simplifies managing boolean state.
62
+
63
+ ```tsx
64
+ import { useBoolState } from 'better-preact-use-state';
65
+
66
+ const MyComponent = () => {
67
+ const [isOpen, { setTrue: open, setFalse: close, toggleState }] =
68
+ useBoolState(false);
38
69
 
39
70
  return (
40
71
  <div>
41
- <p>Count: {count}</p>
42
- <button onClick={incrementCount}>Increment</button>
43
-
44
- <p>Name: {name}</p>
45
- <input
46
- type='text'
47
- value={name}
48
- onChange={(e) => updateName(e.target.value)}
49
- />
72
+ <p>{`Is Open: ${isOpen ? 'Yes' : 'No'}`}</p>
73
+ <button onClick={open}>{'Open'}</button>
74
+ <button onClick={close}>{'Close'}</button>
75
+ <button onClick={toggleState}>{'Toggle'}</button>
76
+ {/* Toggles the boolean value */}
50
77
  </div>
51
78
  );
52
79
  };
@@ -54,6 +81,8 @@ const MyComponent: FunctionalComponent = () => {
54
81
 
55
82
  ## API Reference
56
83
 
84
+ ### `useState`
85
+
57
86
  ```typescript
58
87
  const [state, setState, { updateState, resetState }] = useState<T>(initialState: T);
59
88
  ```
@@ -63,7 +92,7 @@ const [state, setState, { updateState, resetState }] = useState<T>(initialState:
63
92
  - `updateState`: Function to update the state based on the current state.
64
93
  - `resetState`: Function to reset the state to the initial value.
65
94
 
66
- ### `updateState`
95
+ #### `updateState`
67
96
 
68
97
  ```typescript
69
98
  updateState(updateFn: (v: T) => T): void;
@@ -71,7 +100,53 @@ updateState(updateFn: (v: T) => T): void;
71
100
 
72
101
  Updates the state by passing a function `updateFn` that takes the current state as an argument and returns the new state.
73
102
 
74
- ### `resetState`
103
+ #### `resetState`
104
+
105
+ ```typescript
106
+ resetState(): void;
107
+ ```
108
+
109
+ Resets the state to the initial value.
110
+
111
+ ### `useBoolState`
112
+
113
+ ```typescript
114
+ const [state, { setState, setTrue, setFalse, resetState, toggleState, updateState }] = useBoolState(initialState: boolean);
115
+ ```
116
+
117
+ - `state`: The current boolean value.
118
+ - `setState`: Function to update the boolean value directly.
119
+ - `setTrue`: Function to set the state to `true`.
120
+ - `setFalse`: Function to set the state to `false`.
121
+ - `resetState`: Function to reset the state to the initial value.
122
+ - `toggleState`: Function to toggle the boolean value (true to false, false to true).
123
+ - `updateState`: Function to update the state based on the current state.
124
+
125
+ #### `setState`
126
+
127
+ ```typescript
128
+ setState(next: boolean): void;
129
+ ```
130
+
131
+ Sets the state to the provided boolean value.
132
+
133
+ #### `setTrue`
134
+
135
+ ```typescript
136
+ setTrue(): void;
137
+ ```
138
+
139
+ Sets the state to `true`.
140
+
141
+ #### `setFalse`
142
+
143
+ ```typescript
144
+ setFalse(): void;
145
+ ```
146
+
147
+ Sets the state to `false`.
148
+
149
+ #### `resetState`
75
150
 
76
151
  ```typescript
77
152
  resetState(): void;
@@ -79,21 +154,37 @@ resetState(): void;
79
154
 
80
155
  Resets the state to the initial value.
81
156
 
157
+ #### `toggleState`
158
+
159
+ ```typescript
160
+ toggleState(): void;
161
+ ```
162
+
163
+ Toggles the current boolean value.
164
+
165
+ #### `updateState`
166
+
167
+ ```typescript
168
+ updateState(updateFn: (v: boolean) => boolean): void;
169
+ ```
170
+
171
+ Updates the state by passing a function `updateFn` that takes the current state as an argument and returns the new state. This is useful for updates that depend on the current state.
172
+
82
173
  ## Benefits of this library
83
174
 
84
- With Preact's standard `useState`, attempting to store a function as state can lead to unexpected behavior (the function being interpreted as an updater function). `better-preact-use-state` solves this issue and, by providing `updateState`, allows functions to be safely held as state. Also, `resetState` lets you easily return the state to its initial value.
175
+ With Preact's standard `useState`, attempting to store a function as state can lead to unexpected behavior (the function being interpreted as an updater function). `better-preact-use-state` solves this issue and, by providing `updateState`, allows functions to be safely held as state. Also, `resetState` lets you easily return the state to its initial value. `useBoolState` simplifies boolean state management in Preact, offering several advantages over directly using useState for booleans.
85
176
 
86
- ## Example Use Case
177
+ ### Example Use Case
87
178
 
88
179
  Useful when you want to hold a function as state.
89
180
 
90
181
  ```typescript
91
- const [fn, setFn] = useState(initialFn); // Holds a function as state
182
+ const [fn, setFn, { updateState: updateFn }] = useState(initialFn); // Holds a function as state
92
183
 
93
- // With Preact's standard useState, setFn(nextFn) will interpret nextFn as an updater function.
184
+ // In Preact's standard useState, `setFn(nextFn)` interprets nextFn as an update function.
94
185
 
95
- // With better-preact-use-state, you can use updateState to update the function.
96
- updateState((currentFn) => nextFn);
186
+ // If you want to update the state with an update function, you can use updateState instead.
187
+ updateFn((currentFn) => nextFn);
97
188
  ```
98
189
 
99
190
  ## License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-preact-use-state",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "author": "noshiro-pf <noshiro.pf@gmail.com>",