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:
Imre Osswald
2018-11-30 15:02:19 +00:00
committed by Dan Abramov
parent 16e120438c
commit f1bf281605
2 changed files with 34 additions and 1 deletions
+1 -1
View File
@@ -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', () => {