-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
useControlledRerenderState
hook (#865)
- Loading branch information
Showing
8 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/useControlledRerenderState/__docs__/example.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as React from 'react'; | ||
import { useControlledRerenderState, useToggle } from '../..'; | ||
|
||
export const Example: React.FC = () => { | ||
const [state, setState] = useControlledRerenderState(0); | ||
const [doRerender, toggleDoRerender] = useToggle(true); | ||
|
||
return ( | ||
<div> | ||
<div>State: {state}</div> | ||
<p> | ||
<button | ||
onClick={() => { | ||
setState((s) => s + 1, doRerender); | ||
}}> | ||
Increment (+1) | ||
</button>{' '} | ||
<button | ||
onClick={() => { | ||
toggleDoRerender(); | ||
}}> | ||
{doRerender ? 'Disable' : 'Enable'} re-rendering on state set | ||
</button> | ||
</p> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
import { ImportPath } from '../../storybookUtil/ImportPath'; | ||
|
||
<Meta title="State/useControlledRerenderState" component={Example} /> | ||
|
||
# useControlledRerenderState | ||
|
||
Like `React.useState`, but its state setter accepts extra argument, that allows to cancel rerender. | ||
|
||
- Allows to avoid rerender during state set. | ||
|
||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
export type ControlledRerenderDispatch<A> = (value: A, rerender?: boolean) => void; | ||
|
||
export function useControlledRerenderState<S>( | ||
initialState: S | (() => S) | ||
): [S, ControlledRerenderDispatch<React.SetStateAction<S>>]; | ||
export function useControlledRerenderState<S = undefined>(): [ | ||
S | undefined, | ||
ControlledRerenderDispatch<React.SetStateAction<S | undefined>> | ||
]; | ||
``` | ||
|
||
#### Importing | ||
|
||
<ImportPath /> | ||
|
||
#### Arguments | ||
|
||
Identical to `React.useState`. | ||
|
||
#### Return | ||
|
||
Returned state setter accepts extra-argument, in case it is set to false - component will not be | ||
re-rendered after state set. | ||
In case extra parameter omitted - state setter behaves exactly as native. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useControlledRerenderState } from '../..'; | ||
|
||
describe('useControlledRerenderState', () => { | ||
it('should be defined', () => { | ||
expect(useControlledRerenderState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useControlledRerenderState()); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should behave as `useState` by default', () => { | ||
const { result } = renderHook(() => useControlledRerenderState(() => 0)); | ||
|
||
expect(result.current[0]).toBe(0); | ||
|
||
act(() => { | ||
result.current[1](1); | ||
}); | ||
expect(result.current[0]).toBe(1); | ||
|
||
act(() => { | ||
result.current[1]((i) => i + 3); | ||
}); | ||
expect(result.current[0]).toBe(4); | ||
}); | ||
|
||
it('should not re-render in case setter extra-argument set to false', () => { | ||
const { result } = renderHook(() => useControlledRerenderState(() => 0)); | ||
|
||
expect(result.current[0]).toBe(0); | ||
|
||
act(() => { | ||
result.current[1](1, false); | ||
}); | ||
expect(result.current[0]).toBe(0); | ||
|
||
act(() => { | ||
result.current[1]((i) => i + 3); | ||
}); | ||
expect(result.current[0]).toBe(4); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useControlledRerenderState } from '../..'; | ||
|
||
describe('useControlledRerenderState', () => { | ||
it('should be defined', () => { | ||
expect(useControlledRerenderState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useControlledRerenderState()); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
src/useControlledRerenderState/useControlledRerenderState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { SetStateAction, useCallback, useRef } from 'react'; | ||
import { useFirstMountState, useRerender } from '..'; | ||
import { resolveHookState } from '../util/resolveHookState'; | ||
|
||
export type ControlledRerenderDispatch<A> = (value: A, rerender?: boolean) => void; | ||
|
||
export function useControlledRerenderState<S>( | ||
initialState: S | (() => S) | ||
): [S, ControlledRerenderDispatch<SetStateAction<S>>]; | ||
export function useControlledRerenderState<S = undefined>(): [ | ||
S | undefined, | ||
ControlledRerenderDispatch<SetStateAction<S | undefined>> | ||
]; | ||
|
||
/** | ||
* Like `React.useState`, but its state setter accepts extra argument, that allows to cancel | ||
* rerender. | ||
*/ | ||
export function useControlledRerenderState<S>( | ||
initialState?: S | (() => S) | ||
): [S | undefined, ControlledRerenderDispatch<SetStateAction<S | undefined>>] { | ||
const state = useRef<S | undefined>( | ||
useFirstMountState() ? resolveHookState(initialState) : undefined | ||
); | ||
const rr = useRerender(); | ||
|
||
return [ | ||
state.current, | ||
useCallback((value, rerender) => { | ||
const newState = resolveHookState(value, state.current); | ||
|
||
if (newState !== state.current) { | ||
state.current = newState; | ||
|
||
if (typeof rerender === 'undefined' || rerender) { | ||
rr(); | ||
} | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters