mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Fix bug in cloneHook (#14364)
* Fixes #14360 and adds a test for mixed priority dispatches. It was broken because `cloneHook` assigned `memoizedState` instead of `baseState` from the original hook to `baseState` of the clone. * tweak comments
This commit is contained in:
committed by
Dan Abramov
parent
16e120438c
commit
f1bf281605
+1
-1
@@ -253,7 +253,7 @@ function cloneHook(hook: Hook): Hook {
|
||||
return {
|
||||
memoizedState: hook.memoizedState,
|
||||
|
||||
baseState: hook.memoizedState,
|
||||
baseState: hook.baseState,
|
||||
queue: hook.queue,
|
||||
baseUpdate: hook.baseUpdate,
|
||||
|
||||
|
||||
@@ -565,6 +565,39 @@ describe('ReactHooksWithNoopRenderer', () => {
|
||||
ReactNoop.flush();
|
||||
expect(ReactNoop.getChildren()).toEqual([span('Count: 8')]);
|
||||
});
|
||||
|
||||
// Regression test for https://github.com/facebook/react/issues/14360
|
||||
it('handles dispatches with mixed priorities', () => {
|
||||
const INCREMENT = 'INCREMENT';
|
||||
|
||||
function reducer(state, action) {
|
||||
return action === INCREMENT ? state + 1 : state;
|
||||
}
|
||||
|
||||
function Counter(props, ref) {
|
||||
const [count, dispatch] = useReducer(reducer, 0);
|
||||
useImperativeMethods(ref, () => ({dispatch}));
|
||||
return <Text text={'Count: ' + count} />;
|
||||
}
|
||||
|
||||
Counter = forwardRef(Counter);
|
||||
const counter = React.createRef(null);
|
||||
ReactNoop.render(<Counter ref={counter} />);
|
||||
|
||||
ReactNoop.flush();
|
||||
expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]);
|
||||
|
||||
counter.current.dispatch(INCREMENT);
|
||||
counter.current.dispatch(INCREMENT);
|
||||
counter.current.dispatch(INCREMENT);
|
||||
ReactNoop.flushSync(() => {
|
||||
counter.current.dispatch(INCREMENT);
|
||||
});
|
||||
expect(ReactNoop.getChildren()).toEqual([span('Count: 1')]);
|
||||
|
||||
ReactNoop.flush();
|
||||
expect(ReactNoop.getChildren()).toEqual([span('Count: 4')]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('useEffect', () => {
|
||||
|
||||
Reference in New Issue
Block a user