Skip to content

Commit

Permalink
Fixes facebook#14360 and adds a test for mixed priority dispatches.
Browse files Browse the repository at this point in the history
It was broken because `cloneHook` assigned `memoizedState` instead of
`baseState` from the original hook to `baseState` of the clone.
  • Loading branch information
ioss committed Nov 30, 2018
1 parent 16e1204 commit 6d65d58
Show file tree
Hide file tree
Showing 2 changed files with 38 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,43 @@ describe('ReactHooksWithNoopRenderer', () => {
ReactNoop.flush();
expect(ReactNoop.getChildren()).toEqual([span('Count: 8')]);
});

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} />);

// initial Render should be 'Count: 0'
ReactNoop.flush();
expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]);

// dispatch some low prio increments
counter.current.dispatch(INCREMENT);
counter.current.dispatch(INCREMENT);
counter.current.dispatch(INCREMENT);

// dispatch and apply a high prio increment
ReactNoop.flushSync(() => {
counter.current.dispatch(INCREMENT);
});
expect(ReactNoop.getChildren()).toEqual([span('Count: 1')]);

// apply all increments
ReactNoop.flush();
expect(ReactNoop.getChildren()).toEqual([span('Count: 4')]);
});
});

describe('useEffect', () => {
Expand Down

0 comments on commit 6d65d58

Please sign in to comment.