-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #530
- Loading branch information
Showing
8 changed files
with
101 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Meta } from '@storybook/addon-docs/blocks'; | ||
|
||
<Meta title="State/useFunctionalState" /> | ||
|
||
# useFunctionalState | ||
|
||
Like `useState` but instead of raw state, state getter returned. `useSafeState` is used underneath. | ||
|
||
## Reference | ||
|
||
```ts | ||
export function useFunctionalState<S>( | ||
initialState: S | (() => S) | ||
): [() => S, React.Dispatch<React.SetStateAction<S>>]; | ||
export function useFunctionalState<S = undefined>(): [ | ||
() => S | undefined, | ||
React.Dispatch<React.SetStateAction<S | undefined>> | ||
]; | ||
``` |
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,31 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useFunctionalState } from '../..'; | ||
|
||
describe('useFunctionalState', () => { | ||
it('should be defined', () => { | ||
expect(useFunctionalState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useFunctionalState()); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should return proper values', () => { | ||
const { result } = renderHook(() => useFunctionalState(1)); | ||
expect(result.current[1]).toBeInstanceOf(Function); | ||
expect(result.current[0]).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('should return state getter', () => { | ||
const { result } = renderHook(() => useFunctionalState(1)); | ||
|
||
expect(result.current[0]()).toBe(1); | ||
|
||
act(() => { | ||
result.current[1](2); | ||
}); | ||
|
||
expect(result.current[0]()).toBe(2); | ||
}); | ||
}); |
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,19 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useFunctionalState } from '../..'; | ||
|
||
describe('useFunctionalState', () => { | ||
it('should be defined', () => { | ||
expect(useFunctionalState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useFunctionalState()); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should return proper values', () => { | ||
const { result } = renderHook(() => useFunctionalState(1)); | ||
expect(result.current[1]).toBeInstanceOf(Function); | ||
expect(result.current[0]).toBeInstanceOf(Function); | ||
}); | ||
}); |
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 } from 'react'; | ||
import { useSafeState } from '../useSafeState/useSafeState'; | ||
import { useSyncedRef } from '../useSyncedRef/useSyncedRef'; | ||
|
||
export function useFunctionalState<S>( | ||
initialState: S | (() => S) | ||
): [() => S, Dispatch<SetStateAction<S>>]; | ||
export function useFunctionalState<S = undefined>(): [ | ||
() => S | undefined, | ||
Dispatch<SetStateAction<S | undefined>> | ||
]; | ||
|
||
/** | ||
* Like `useState` but instead of raw state, state getter returned. `useSafeState` is | ||
* used underneath. | ||
*/ | ||
export function useFunctionalState<S>( | ||
initialState?: S | (() => S) | ||
): [() => S | undefined, Dispatch<SetStateAction<S | undefined>>] { | ||
const [state, setState] = useSafeState(initialState); | ||
const stateRef = useSyncedRef(state); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return [useCallback(() => stateRef.current, []), setState]; | ||
} |
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