-
Notifications
You must be signed in to change notification settings - Fork 94
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
feat: new hook useGetSet #530
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as React from 'react'; | ||
import { useGetSet } from '../..'; | ||
|
||
export const Example: React.FC = () => { | ||
const [get, set] = useGetSet(0); | ||
const onClick = () => { | ||
setTimeout(() => { | ||
set(get() + 1); | ||
}, 1000); | ||
}; | ||
return <button onClick={onClick}>Clicked: {get()}</button>; | ||
}; |
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 { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
import { ImportPath } from '../../storybookUtil/ImportPath'; | ||
|
||
<Meta title="State/useGetSet" component={Example} /> | ||
|
||
# useGetSet | ||
|
||
React state hook that returns state getter function instead of raw state itself, this prevents subtle bugs when state is used in nested functions. | ||
|
||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
export function useGetSet<S>( | ||
initialState: IInitialState<S> | ||
): [get: () => S, set: (nextState: INextState<S>) => void]; | ||
``` | ||
|
||
#### Importing | ||
|
||
<ImportPath /> | ||
|
||
#### Arguments | ||
|
||
- _**initialState**_ _`IInitialState<S>`_ - initial state or initial state setter as for `useState` | ||
|
||
#### Return | ||
|
||
Returns array alike `useState` does, but instead return a state value it return a getter function. | ||
|
||
- _**[0]**_ _`S`_ - getter of current state | ||
- _**[1]**_ _`(nextState?: INewState<S>) => void`_ - state setter as for `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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks/dom'; | ||
import { useGetSet } from '../..'; | ||
|
||
const setUp = (initialValue: any) => renderHook(() => useGetSet(initialValue)); | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
describe('useGetSet', () => { | ||
it('should be defined', () => { | ||
expect(useGetSet).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = setUp('foo'); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should init getter and setter', () => { | ||
const { result } = setUp('foo'); | ||
const [get, set] = result.current; | ||
expect(get).toBeInstanceOf(Function); | ||
expect(set).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('should get current value', () => { | ||
const { result } = setUp('foo'); | ||
const [get] = result.current; | ||
expect(get()).toBe('foo'); | ||
}); | ||
|
||
it('should set new value', () => { | ||
const { result } = setUp('foo'); | ||
const [get, set] = result.current; | ||
act(() => set('bar')); | ||
const currentValue = get(); | ||
expect(currentValue).toBe('bar'); | ||
}); | ||
|
||
it('should get and set expected values when used in nested functions', () => { | ||
const { result } = setUp(0); | ||
const [get, set] = result.current; | ||
const onClick = jest.fn(() => { | ||
setTimeout(() => { | ||
set(get() + 1); | ||
}, 1000); | ||
}); | ||
|
||
// simulate 3 clicks | ||
onClick(); | ||
onClick(); | ||
onClick(); | ||
|
||
act(() => { | ||
jest.runAllTimers(); | ||
}); | ||
|
||
expect(get()).toBe(3); | ||
expect(onClick).toHaveBeenCalledTimes(3); | ||
}); | ||
}); |
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,62 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks/server'; | ||
import { useGetSet } from '../..'; | ||
|
||
const setUp = (initialValue: any) => renderHook(() => useGetSet(initialValue)); | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
describe('useGetSet', () => { | ||
it('should be defined', () => { | ||
expect(useGetSet).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = setUp('foo'); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should init getter and setter', () => { | ||
const { result } = setUp('foo'); | ||
const [get, set] = result.current; | ||
expect(get).toBeInstanceOf(Function); | ||
expect(set).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('should get current value', () => { | ||
const { result } = setUp('foo'); | ||
const [get] = result.current; | ||
expect(get()).toBe('foo'); | ||
}); | ||
|
||
it('should set new value', () => { | ||
const { result } = setUp('foo'); | ||
const [get, set] = result.current; | ||
act(() => set('bar')); | ||
const currentValue = get(); | ||
expect(currentValue).toBe('bar'); | ||
}); | ||
|
||
it('should get and set expected values when used in nested functions', () => { | ||
const { result } = setUp(0); | ||
const [get, set] = result.current; | ||
const onClick = jest.fn(() => { | ||
setTimeout(() => { | ||
set(get() + 1); | ||
}, 1000); | ||
}); | ||
|
||
// simulate 3 clicks | ||
onClick(); | ||
onClick(); | ||
onClick(); | ||
|
||
act(() => { | ||
jest.runAllTimers(); | ||
}); | ||
|
||
expect(get()).toBe(3); | ||
expect(onClick).toHaveBeenCalledTimes(3); | ||
}); | ||
}); |
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 { useCallback, Dispatch, SetStateAction } from 'react'; | ||
import { IInitialState } from '../util/resolveHookState'; | ||
import { useSafeState, useSyncedRef } from '../index'; | ||
|
||
export function useGetSet<S>( | ||
initialState: IInitialState<S> | ||
): [get: () => S, set: Dispatch<SetStateAction<S>>] { | ||
const [state, setState] = useSafeState(initialState); | ||
const stateRef = useSyncedRef(state); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return [useCallback(() => stateRef.current, []), setState]; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is way overcomplicated, why not to simply set state inside act?