Skip to content
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

fix: Run reducers with the props from the queued update #21419

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,9 @@ function updateReducer<S, I, A>(
newState = ((update.eagerState: any): S);
} else {
const action = update.action;
newState = reducer(newState, action);
const reducerImpl =
update.eagerReducer !== null ? update.eagerReducer : reducer;
newState = reducerImpl(newState, action);
}
}
update = update.next;
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,9 @@ function updateReducer<S, I, A>(
newState = ((update.eagerState: any): S);
} else {
const action = update.action;
newState = reducer(newState, action);
const reducerImpl =
update.eagerReducer !== null ? update.eagerReducer : reducer;
newState = reducerImpl(newState, action);
}
}
update = update.next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3784,4 +3784,49 @@ describe('ReactHooksWithNoopRenderer', () => {

expect(Scheduler).toHaveYielded(['Render: 0']);
});

it('reducer-closure', () => {
let setLimit;
let increment;
function Test() {
const [limit, _setLimit] = useState(5);
const [count, _increment] = useReducer((state, action) => {
return state < limit ? state + 1 : state;
}, 1);
setLimit = _setLimit;
increment = _increment;

return <Text text={`Render: ${count}`} />;
}

act(() => {
ReactNoop.render(<Test />);
});

expect(Scheduler).toHaveYielded(['Render: 1']);

act(() => {
increment();
increment();
increment();
increment();
});

expect(Scheduler).toHaveYielded(['Render: 5']);

act(() => {
increment();
});
act(() => {
increment();
});

expect(Scheduler).toHaveYielded([]);

act(() => {
setLimit(10);
});

expect(Scheduler).toHaveYielded(['Render: 5']);
});
});