[Fiber] Introduce API to opt-out of batching#8661
Conversation
We may decide to provide this ability as an escape hatch in the future.
bvaughn
left a comment
There was a problem hiding this comment.
I like this change. Cleaner and easier to follow. Small problem with a test but otherwise things look good to me.
|
|
||
| ReactNoop.syncUpdates(() => { | ||
| instance.setState({ step: 1 }); | ||
| expect(ReactNoop.getChildren()).toEqual([span(2)]); |
There was a problem hiding this comment.
This expectation won't be evaluated. It could fail and Jest won't catch it because you've already called done in componentDidUpdate above. To resolve this you could set a bool (eg componentDidUpdateCalled = true) in componentDidUpdate above, verify it here, and then call done() at the end of the sync update.
it('nested updates are always deferred, even inside unbatchedUpdates', done => {
let instance;
let componentDidUpdateCalled = false;
class Foo extends React.Component {
state = { step: 0 };
componentDidUpdate() {
if (this.state.step === 1) {
ReactNoop.unbatchedUpdates(() => {
// This is a nested state update, so it should not be
// flushed synchronously, even though we wrapped it
// in unbatchedUpdates.
this.setState({ step: 2 });
});
expect(ReactNoop.getChildren()).toEqual([span(1)]);
componentDidUpdateCalled = true;
}
}
render() {
instance = this;
return <span prop={this.state.step} />;
}
}
ReactNoop.render(<Foo />);
ReactNoop.flush();
expect(ReactNoop.getChildren()).toEqual([span(0)]);
ReactNoop.syncUpdates(() => {
instance.setState({ step: 1 });
expect(componentDidUpdateCalled).toBe(true);
expect(ReactNoop.getChildren()).toEqual([span(2)]);
done();
});
});There was a problem hiding this comment.
Fixed by pushing to an array like we usually do
Reverses the effect of batchedUpdates by resetting the current batching context. Does not affect nested updates, which are always deferred regardless of whether they are inside a batch.
5a4bbc5 to
ad7bcdf
Compare
| expect(container2.textContent).toEqual('Hello'); | ||
| ReactDOM.unmountComponentAtNode(container2); | ||
| expect(container2.textContent).toEqual(''); | ||
| } |
There was a problem hiding this comment.
You've changed this behavior so you had to update but you didn't replace it with an equivalent test anywhere. This is just less test coverage instead of testing the new behavior.
There was a problem hiding this comment.
unbatchedUpdatesreverses the effect ofbatchedUpdatesby resetting the batching context.This does not affect nested updates, which are always deferred regardless of whether they are inside a batch.
This change is a partial reversion of #8634, which added the ability to trigger nested updates using
syncUpdates. I had thought that was the behavior we wanted for top-level mount and unmount, but it turns out all we really need is the ability to opt-out ofbatchedUpdates. So even though it's a deviation from how Stack works, Fiber does not allow nested updates at all. We may later decide to provide this as an escape hatch, if it helps people migrate to Fiber.