-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>; | ||
}; |
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`. |
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); | ||
}); | ||
}); |
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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useRef, useCallback } from 'react'; | ||
import { IInitialState, INextState, resolveHookState } from '../util/resolveHookState'; | ||
import { useRerender } from '../index'; | ||
|
||
export function useGetSet<S>( | ||
initialState: IInitialState<S> | ||
): [get: () => S, set: (nextState: INextState<S>) => void] { | ||
const rerender = useRerender(); | ||
const ref = useRef<S>(resolveHookState(initialState)); | ||
const get = useCallback(() => ref.current, []); | ||
const set = useCallback( | ||
(nextState: INextState<S>) => { | ||
ref.current = resolveHookState(nextState, ref.current); | ||
rerender(); | ||
}, | ||
[rerender] | ||
); | ||
return [get, set]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not to use Besides this code will work - id prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't understand here.
what is it means here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either useState nor useSafeState update the value immediately. That why this hook is need and How There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation breaks 'state flow' of react, and will break in strict mode, 4ex. What i propose is to implement hook like this: 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];
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation doesn't return latest value in getter. / There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make sense to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But how to write test, since I don't know when will react state change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wrap all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, PR lack of readme changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This test cannot pass / |
||
} |
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?