Skip to content

Commit

Permalink
Fix bug in cloneHook (facebook#14364)
Browse files Browse the repository at this point in the history
* Fixes facebook#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
  • Loading branch information
ioss authored and n8schloss committed Jan 31, 2019
1 parent 4119b7e commit d3559d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit d3559d3

Please sign in to comment.