Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useResetState): random initial value changed #2308

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions packages/hooks/src/useResetState/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { act, renderHook } from '@testing-library/react';
import useResetState from '../index';

describe('useResetState', () => {
const setUp = <S>(initialState: S) =>
const setUp = (initialState) =>
renderHook(() => {
const [state, setState, resetState] = useResetState<S>(initialState);
const [state, setState, resetState] = useResetState(initialState);

return {
state,
Expand All @@ -20,33 +20,60 @@ describe('useResetState', () => {
expect(hook.result.current.state).toEqual({ hello: 'world' });
});

it('should support functional initialValue', () => {
const hook = setUp(() => ({
hello: 'world',
}));
expect(hook.result.current.state).toEqual({ hello: 'world' });
});

it('should reset state', () => {
const hook = setUp({
hello: '',
count: 0,
});

act(() => {
hook.result.current.setState({
hello: 'world',
count: 1,
});
});

act(() => {
hook.result.current.setState({ hello: 'world', count: 1 });
hook.result.current.resetState();
});

expect(hook.result.current.state).toEqual({ hello: '', count: 0 });
});

it('should support function update', () => {
const hook = setUp({
count: 0,
});

act(() => {
hook.result.current.setState((prev) => ({ count: prev.count + 1 }));
});
expect(hook.result.current.state).toEqual({ count: 1 });
});

it('should keep random initial state', () => {
const random = Math.random();
const hook = setUp({
count: random,
});

act(() => {
hook.result.current.setState({ count: Math.random() });
hook.result.current.resetState();
});
expect(hook.result.current.state).toEqual({ count: random });
});

it('should support random functional initialValue', () => {
const random = Math.random();
const hook = setUp(() => ({
count: random,
}));

act(() => {
hook.result.current.setState({ count: Math.random() });
hook.result.current.resetState();
});
expect(hook.result.current.state).toEqual({ count: random });
});
});
19 changes: 12 additions & 7 deletions packages/hooks/src/useResetState/demo/demo1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ import { useResetState } from 'ahooks';

interface State {
hello: string;
count: number;
value: number;
}

const initialValue = {
hello: '',
value: Math.random(),
};

export default () => {
const [state, setState, resetState] = useResetState<State>({
hello: '',
count: 0,
});
const [state, setState, resetState] = useResetState<State>(initialValue);

return (
<div>
<div>initial state:</div>
<pre>{JSON.stringify(initialValue, null, 2)}</pre>
<div>current state:</div>
<pre>{JSON.stringify(state, null, 2)}</pre>
<p>
<button
type="button"
style={{ marginRight: '8px' }}
onClick={() => setState({ hello: 'world', count: 1 })}
onClick={() => setState(() => ({ hello: 'world', value: Math.random() }))}
>
set hello and count
set hello and value
</button>

<button type="button" onClick={resetState}>
Expand Down
11 changes: 9 additions & 2 deletions packages/hooks/src/useResetState/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { useState } from 'react';
import type { Dispatch, SetStateAction } from 'react';
import useMemoizedFn from '../useMemoizedFn';
import { isFunction } from '../utils';
import useCreation from '../useCreation';

type ResetState = () => void;

const useResetState = <S>(
initialState: S | (() => S),
): [S, Dispatch<SetStateAction<S>>, ResetState] => {
const [state, setState] = useState(initialState);
const initialStateMemo = useCreation(
() => (isFunction(initialState) ? initialState() : initialState),
[],
);

const [state, setState] = useState(initialStateMemo);

const resetState = useMemoizedFn(() => {
setState(initialState);
setState(initialStateMemo);
});

return [state, setState, resetState];
Expand Down
Loading