Skip to content

[Bugfix] Prevent already-committed setState callback from firing again during a rebase #21498

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

Merged
merged 2 commits into from
May 12, 2021
Merged
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
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactUpdateQueue.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,12 @@ export function processUpdateQueue<State>(
instance,
);
const callback = update.callback;
if (callback !== null) {
if (
callback !== null &&
// If the update was already committed, we should not queue its
// callback again.
update.lane !== NoLane
) {
workInProgress.flags |= Callback;
const effects = queue.effects;
if (effects === null) {
Expand Down
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactUpdateQueue.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,12 @@ export function processUpdateQueue<State>(
instance,
);
const callback = update.callback;
if (callback !== null) {
if (
callback !== null &&
// If the update was already committed, we should not queue its
// callback again.
update.lane !== NoLane
) {
workInProgress.flags |= Callback;
const effects = queue.effects;
if (effects === null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
let React;
let ReactNoop;
let Scheduler;

describe('ReactClassSetStateCallback', () => {
beforeEach(() => {
jest.resetModules();

React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
});

function Text({text}) {
Scheduler.unstable_yieldValue(text);
return text;
}

test('regression: setState callback (2nd arg) should only fire once, even after a rebase', async () => {
let app;
class App extends React.Component {
state = {step: 0};
render() {
app = this;
return <Text text={this.state.step} />;
}
}

const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded([0]);

await ReactNoop.act(async () => {
app.setState({step: 1}, () =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be wrapped in startTransition for the default sync case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this test it only matters that it's lower priority than discrete/sync (default sync !== discrete sync)

Scheduler.unstable_yieldValue('Callback 1'),
);
ReactNoop.flushSync(() => {
app.setState({step: 2}, () =>
Scheduler.unstable_yieldValue('Callback 2'),
);
});
});
expect(Scheduler).toHaveYielded([2, 'Callback 2', 2, 'Callback 1']);
});
});