better-preact-use-state 1.0.0 → 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 +192 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,192 @@
1
+ ## better-preact-use-state
2
+
3
+ A utility that improves Preact's `useState` to be safer and more convenient. Provides both `useState` (enhanced) and a dedicated `useBoolState` hook.
4
+
5
+ ## Overview
6
+
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
+
9
+ ## Installation
10
+
11
+ You can install it with any of the following:
12
+
13
+ ```bash
14
+ npm install better-preact-use-state
15
+ ```
16
+
17
+ ```bash
18
+ yarn add better-preact-use-state
19
+ ```
20
+
21
+ ```bash
22
+ pnpm add better-preact-use-state
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### `useState`
28
+
29
+ `better-preact-use-state`'s `useState` can be used the same way as Preact's `useState`.
30
+
31
+ ```tsx
32
+ import { useState } from 'better-preact-use-state';
33
+
34
+ const MyComponent: FunctionalComponent = () => {
35
+ const [name, setName] = useState('John Doe');
36
+
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);
69
+
70
+ return (
71
+ <div>
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 */}
77
+ </div>
78
+ );
79
+ };
80
+ ```
81
+
82
+ ## API Reference
83
+
84
+ ### `useState`
85
+
86
+ ```typescript
87
+ const [state, setState, { updateState, resetState }] = useState<T>(initialState: T);
88
+ ```
89
+
90
+ - `state`: The current state.
91
+ - `setState`: Function to update the state.
92
+ - `updateState`: Function to update the state based on the current state.
93
+ - `resetState`: Function to reset the state to the initial value.
94
+
95
+ #### `updateState`
96
+
97
+ ```typescript
98
+ updateState(updateFn: (v: T) => T): void;
99
+ ```
100
+
101
+ Updates the state by passing a function `updateFn` that takes the current state as an argument and returns the new state.
102
+
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`
150
+
151
+ ```typescript
152
+ resetState(): void;
153
+ ```
154
+
155
+ Resets the state to the initial value.
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
+
173
+ ## Benefits of this library
174
+
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.
176
+
177
+ ### Example Use Case
178
+
179
+ Useful when you want to hold a function as state.
180
+
181
+ ```typescript
182
+ const [fn, setFn, { updateState: updateFn }] = useState(initialFn); // Holds a function as state
183
+
184
+ // In Preact's standard useState, `setFn(nextFn)` interprets nextFn as an update function.
185
+
186
+ // If you want to update the state with an update function, you can use updateState instead.
187
+ updateFn((currentFn) => nextFn);
188
+ ```
189
+
190
+ ## License
191
+
192
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-preact-use-state",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "author": "noshiro-pf <noshiro.pf@gmail.com>",