Skip to content

Commit 1b4e2fc

Browse files
authored
docs(readme): update docs (#11)
1 parent 95323fa commit 1b4e2fc

File tree

6 files changed

+71
-83
lines changed

6 files changed

+71
-83
lines changed

README.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,14 @@ Provide you can create immutable state easily with mutable way.
3333
import { useMutative } from 'use-mutative';
3434

3535
export function App() {
36-
const [state, mutateState] = useMutative({
37-
foo: 'bar',
38-
list: [{ text: 'todo' }],
39-
});
36+
const [todos, setTodos] = useMutative([{ text: 'todo' }]);
4037
return (
4138
<>
4239
<button
4340
onClick={() => {
44-
// set value with draft mutable
45-
mutateState((draft) => {
46-
draft.foo = `${draft.foo} 2`;
47-
draft.list.push({ text: 'todo 2' });
41+
// set todos with draft mutable
42+
setTodos((draft) => {
43+
draft.push({ text: 'todo 2' });
4844
});
4945
}}
5046
>
@@ -53,10 +49,7 @@ export function App() {
5349
<button
5450
onClick={() => {
5551
// also can override value directly
56-
mutateState({
57-
foo: 'bar 2',
58-
list: [{ text: 'todo 2' }],
59-
});
52+
setTodos([{ text: 'todo' }, { text: 'todo 2' }]);
6053
}}
6154
>
6255
click
@@ -76,7 +69,6 @@ Provide you can create immutable state easily with mutable way in reducer way.
7669
import { rawReturn } from 'mutative';
7770
import { useMutativeReducer } from 'use-mutative';
7871

79-
8072
const initialState = {
8173
count: 0,
8274
};
@@ -117,7 +109,7 @@ More detail about `use-mutative` can be found in [API docs](https://github.com/m
117109
In some cases, you may want to get that patches from your update, we can pass `{ enablePatches: true }` options in `useMutative()` or `useMutativeReducer()`, that can provide you the ability to get that patches from pervious action.
118110

119111
```tsx
120-
const [state, mutateState, patches, inversePatches] = useMutative(initState, {
112+
const [state, updateState, patches, inversePatches] = useMutative(initState, {
121113
enablePatches: true,
122114
});
123115

docs/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Provide you can create immutable state easily with mutable way.
3535
import { useMutative } from 'use-mutative';
3636

3737
export function App() {
38-
const [state, mutateState] = useMutative({
38+
const [state, setState] = useMutative({
3939
foo: 'bar',
4040
list: [{ text: 'todo' }],
4141
});
@@ -44,7 +44,7 @@ export function App() {
4444
<button
4545
onClick={() => {
4646
// set value with draft mutable
47-
mutateState((draft) => {
47+
setState((draft) => {
4848
draft.foo = `${draft.foo} 2`;
4949
draft.list.push({ text: 'todo 2' });
5050
});
@@ -55,7 +55,7 @@ export function App() {
5555
<button
5656
onClick={() => {
5757
// also can override value directly
58-
mutateState({
58+
setState({
5959
foo: 'bar 2',
6060
list: [{ text: 'todo 2' }],
6161
});
@@ -118,7 +118,7 @@ More detail about `use-mutative` can be found in [API docs](https://github.com/m
118118
In some cases, you may want to get that patches from your update, we can pass `{ enablePatches: true }` options in `useMutative` or `useMutativeReducer`, that can provide you the ability to get that patches from pervious action.
119119

120120
```tsx
121-
const [state, mutateState, patches, inversePatches] = useMutative(initState, {
121+
const [state, setState, patches, inversePatches] = useMutative(initState, {
122122
enablePatches: true,
123123
});
124124

docs/modules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ import { act, renderHook } from '@testing-library/react';
142142
import { useMutative } from '../src/index';
143143

144144
const { result } = renderHook(() => useMutative({ items: [1] }));
145-
const [state, mutateState] = result.current;
145+
const [state, setState] = result.current;
146146
act(() =>
147-
mutateState((draft) => {
147+
setState((draft) => {
148148
draft.items.push(2);
149149
})
150150
);

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ type Result<S, O extends PatchesOptions, F extends boolean> = O extends
5252
* import { useMutative } from '../src/index';
5353
*
5454
* const { result } = renderHook(() => useMutative({ items: [1] }));
55-
* const [state, mutateState] = result.current;
55+
* const [state, updateState] = result.current;
5656
* act(() =>
57-
* mutateState((draft) => {
57+
* updateState((draft) => {
5858
* draft.items.push(2);
5959
* })
6060
* );
@@ -94,11 +94,11 @@ function useMutative<
9494
currentCount += 1;
9595
renderCount.current += 1;
9696
//#endregion
97-
const [state, mutateState] = useState(() =>
97+
const [state, setState] = useState(() =>
9898
typeof initialValue === 'function' ? initialValue() : initialValue
9999
);
100100
const updateState = useCallback((updater: any) => {
101-
mutateState((latest: any) => {
101+
setState((latest: any) => {
102102
const updaterFn = typeof updater === 'function' ? updater : () => updater;
103103
const result = create(latest, updaterFn, options);
104104
if (options?.enablePatches) {
@@ -108,7 +108,7 @@ function useMutative<
108108
renderCount.current === count.current + 1
109109
) {
110110
Array.prototype.push.apply(patchesRef.current.patches, result[1]);
111-
// `inversePatches` should be in reverse order when multiple mutateState() executions
111+
// `inversePatches` should be in reverse order when multiple setState() executions
112112
Array.prototype.unshift.apply(
113113
patchesRef.current.inversePatches,
114114
result[2]
@@ -277,7 +277,7 @@ function useMutativeReducer<
277277
renderCount.current === count.current + 1
278278
) {
279279
Array.prototype.push.apply(patchesRef.current.patches, result[1]);
280-
// `inversePatches` should be in reverse order when multiple mutateState() executions
280+
// `inversePatches` should be in reverse order when multiple setState() executions
281281
Array.prototype.unshift.apply(
282282
patchesRef.current.inversePatches,
283283
result[2]

test/index.0.snap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { act, renderHook } from '@testing-library/react';
33
import { useMutative } from '../src/index';
44

55
const { result } = renderHook(() => useMutative({ items: [1] }));
6-
const [state, mutateState] = result.current;
6+
const [state, updateState] = result.current;
77
act(() =>
8-
mutateState((draft) => {
8+
updateState((draft) => {
99
draft.items.push(2);
1010
})
1111
);

0 commit comments

Comments
 (0)