Skip to content

Commit

Permalink
refeator(name): change setState convention to mutateState (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvliwanag authored May 30, 2024
1 parent a15079b commit 95323fa
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 69 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Provide you can create immutable state easily with mutable way.
import { useMutative } from 'use-mutative';

export function App() {
const [state, setState] = useMutative({
const [state, mutateState] = useMutative({
foo: 'bar',
list: [{ text: 'todo' }],
});
Expand All @@ -42,7 +42,7 @@ export function App() {
<button
onClick={() => {
// set value with draft mutable
setState((draft) => {
mutateState((draft) => {
draft.foo = `${draft.foo} 2`;
draft.list.push({ text: 'todo 2' });
});
Expand All @@ -53,7 +53,7 @@ export function App() {
<button
onClick={() => {
// also can override value directly
setState({
mutateState({
foo: 'bar 2',
list: [{ text: 'todo 2' }],
});
Expand Down Expand Up @@ -117,7 +117,7 @@ More detail about `use-mutative` can be found in [API docs](https://github.com/m
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.

```tsx
const [state, setState, patches, inversePatches] = useMutative(initState, {
const [state, mutateState, patches, inversePatches] = useMutative(initState, {
enablePatches: true,
});

Expand Down
8 changes: 4 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Provide you can create immutable state easily with mutable way.
import { useMutative } from 'use-mutative';

export function App() {
const [state, setState] = useMutative({
const [state, mutateState] = useMutative({
foo: 'bar',
list: [{ text: 'todo' }],
});
Expand All @@ -44,7 +44,7 @@ export function App() {
<button
onClick={() => {
// set value with draft mutable
setState((draft) => {
mutateState((draft) => {
draft.foo = `${draft.foo} 2`;
draft.list.push({ text: 'todo 2' });
});
Expand All @@ -55,7 +55,7 @@ export function App() {
<button
onClick={() => {
// also can override value directly
setState({
mutateState({
foo: 'bar 2',
list: [{ text: 'todo 2' }],
});
Expand Down Expand Up @@ -118,7 +118,7 @@ More detail about `use-mutative` can be found in [API docs](https://github.com/m
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.

```tsx
const [state, setState, patches, inversePatches] = useMutative(initState, {
const [state, mutateState, patches, inversePatches] = useMutative(initState, {
enablePatches: true,
});

Expand Down
4 changes: 2 additions & 2 deletions docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ import { act, renderHook } from '@testing-library/react';
import { useMutative } from '../src/index';

const { result } = renderHook(() => useMutative({ items: [1] }));
const [state, setState] = result.current;
const [state, mutateState] = result.current;
act(() =>
setState((draft) => {
mutateState((draft) => {
draft.items.push(2);
})
);
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ type Result<S, O extends PatchesOptions, F extends boolean> = O extends
* import { useMutative } from '../src/index';
*
* const { result } = renderHook(() => useMutative({ items: [1] }));
* const [state, setState] = result.current;
* const [state, mutateState] = result.current;
* act(() =>
* setState((draft) => {
* mutateState((draft) => {
* draft.items.push(2);
* })
* );
Expand Down Expand Up @@ -94,11 +94,11 @@ function useMutative<
currentCount += 1;
renderCount.current += 1;
//#endregion
const [state, setState] = useState(() =>
const [state, mutateState] = useState(() =>
typeof initialValue === 'function' ? initialValue() : initialValue
);
const updateState = useCallback((updater: any) => {
setState((latest: any) => {
mutateState((latest: any) => {
const updaterFn = typeof updater === 'function' ? updater : () => updater;
const result = create(latest, updaterFn, options);
if (options?.enablePatches) {
Expand All @@ -108,7 +108,7 @@ function useMutative<
renderCount.current === count.current + 1
) {
Array.prototype.push.apply(patchesRef.current.patches, result[1]);
// `inversePatches` should be in reverse order when multiple setState() executions
// `inversePatches` should be in reverse order when multiple mutateState() executions
Array.prototype.unshift.apply(
patchesRef.current.inversePatches,
result[2]
Expand Down Expand Up @@ -277,7 +277,7 @@ function useMutativeReducer<
renderCount.current === count.current + 1
) {
Array.prototype.push.apply(patchesRef.current.patches, result[1]);
// `inversePatches` should be in reverse order when multiple setState() executions
// `inversePatches` should be in reverse order when multiple mutateState() executions
Array.prototype.unshift.apply(
patchesRef.current.inversePatches,
result[2]
Expand Down
4 changes: 2 additions & 2 deletions test/index.0.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { act, renderHook } from '@testing-library/react';
import { useMutative } from '../src/index';

const { result } = renderHook(() => useMutative({ items: [1] }));
const [state, setState] = result.current;
const [state, mutateState] = result.current;
act(() =>
setState((draft) => {
mutateState((draft) => {
draft.items.push(2);
})
);
Expand Down
Loading

0 comments on commit 95323fa

Please sign in to comment.