better-preact-use-state 1.0.0 → 1.0.1
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.
- package/README.md +101 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# better-preact-use-state
|
|
2
|
+
|
|
3
|
+
A utility that improves Preact's `useState` to be safer and more convenient.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
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.
|
|
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
|
+
`better-preact-use-state` can be used the same way as Preact's `useState`.
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import { useState } from 'better-preact-use-state';
|
|
31
|
+
|
|
32
|
+
const MyComponent: FunctionalComponent = () => {
|
|
33
|
+
const [count, setCount] = useState(0);
|
|
34
|
+
const [name, setName] = useState('John Doe');
|
|
35
|
+
|
|
36
|
+
const incrementCount = () => setCount(count + 1);
|
|
37
|
+
const updateName = (newName: string) => setName(newName);
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<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
|
+
/>
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API Reference
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
const [state, setState, { updateState, resetState }] = useState<T>(initialState: T);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- `state`: The current state.
|
|
62
|
+
- `setState`: Function to update the state.
|
|
63
|
+
- `updateState`: Function to update the state based on the current state.
|
|
64
|
+
- `resetState`: Function to reset the state to the initial value.
|
|
65
|
+
|
|
66
|
+
### `updateState`
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
updateState(updateFn: (v: T) => T): void;
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Updates the state by passing a function `updateFn` that takes the current state as an argument and returns the new state.
|
|
73
|
+
|
|
74
|
+
### `resetState`
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
resetState(): void;
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Resets the state to the initial value.
|
|
81
|
+
|
|
82
|
+
## Benefits of this library
|
|
83
|
+
|
|
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.
|
|
85
|
+
|
|
86
|
+
## Example Use Case
|
|
87
|
+
|
|
88
|
+
Useful when you want to hold a function as state.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
const [fn, setFn] = useState(initialFn); // Holds a function as state
|
|
92
|
+
|
|
93
|
+
// With Preact's standard useState, setFn(nextFn) will interpret nextFn as an updater function.
|
|
94
|
+
|
|
95
|
+
// With better-preact-use-state, you can use updateState to update the function.
|
|
96
|
+
updateState((currentFn) => nextFn);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
MIT
|