-
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: new hook `useSafeState` * fix: some typos
- Loading branch information
Showing
19 changed files
with
149 additions
and
29 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
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
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,25 @@ | ||
import { Dispatch, SetStateAction, useCallback, useState } from 'react'; | ||
import { useIsMounted } from './useIsMounted'; | ||
|
||
/** | ||
* Like `useState` but its state setter is guarded against sets on unmounted component. | ||
* | ||
* @param initialState | ||
*/ | ||
export function useSafeState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>]; | ||
export function useSafeState<S = undefined>(): [ | ||
S | undefined, | ||
Dispatch<SetStateAction<S | undefined>> | ||
]; | ||
export function useSafeState<S>(initialState?: S | (() => S)): [S, Dispatch<SetStateAction<S>>] { | ||
const [state, setState] = useState(initialState); | ||
const isMounted = useIsMounted(); | ||
|
||
return [ | ||
state, | ||
useCallback((value) => { | ||
if (isMounted()) setState(value); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) as Dispatch<SetStateAction<S>>, | ||
]; | ||
} |
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
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
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
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
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,22 @@ | ||
import { Meta } from '@storybook/addon-docs/blocks'; | ||
|
||
<Meta title="State/useSafeState" /> | ||
|
||
# useSafeState | ||
|
||
Have you ever been caught on `Can't perform a React state update on an unmounted component.`? Of | ||
course you do, we're all been there😅 | ||
Async callback invoked, it tries to set state, but component is already unmounted, that annoying | ||
warning happens, and you have to track component mount state manually. | ||
|
||
`useSafeState` covers your back - it tracks component mount state and does not perform `setState` | ||
action if component is unmounted, otherwise it is the same hook as common `useState`. | ||
|
||
#### Example | ||
|
||
Sadly we can't provide an example since this documentation built in `production` mode and warning | ||
are only shown in `development` mode. | ||
|
||
## Reference | ||
|
||
Use it exactly the same as `useState`. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useSafeState } from '../../src'; | ||
|
||
describe('useSafeState', () => { | ||
it('should be defined', () => { | ||
expect(useSafeState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
renderHook(() => useSafeState()); | ||
}); | ||
|
||
it('should not call ', () => { | ||
const consoleSpy = jest.spyOn(console, 'error'); | ||
consoleSpy.mockImplementationOnce(() => {}); | ||
|
||
const { result, unmount } = renderHook(() => useSafeState(1)); | ||
expect(result.current[1]).toBeInstanceOf(Function); | ||
expect(result.current[0]).toBe(1); | ||
|
||
act(() => { | ||
result.current[1](321); | ||
}); | ||
|
||
expect(result.current[0]).toBe(321); | ||
|
||
unmount(); | ||
|
||
act(() => { | ||
result.current[1](123); | ||
}); | ||
|
||
expect(consoleSpy).toHaveBeenCalledTimes(0); | ||
expect(result.current[0]).toBe(321); | ||
|
||
consoleSpy.mockRestore(); | ||
}); | ||
}); |
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,18 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useSafeState } from '../../src'; | ||
|
||
describe('useSafeState', () => { | ||
it('should be defined', () => { | ||
expect(useSafeState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
renderHook(() => useSafeState()); | ||
}); | ||
|
||
it('should not call ', () => { | ||
const { result } = renderHook(() => useSafeState(1)); | ||
expect(result.current[1]).toBeInstanceOf(Function); | ||
expect(result.current[0]).toBe(1); | ||
}); | ||
}); |