-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
flushDiscreteUpdates
from end of event (#21223)
We don't need this anymore because we flush in a microtask. This should allow us to remove the logic in the event system that tracks nested event dispatches. I added a test to confirm that nested event dispatches don't triggger a synchronous flush, like they would if we wrapped them `flushSync`. It already passed; I added it to prevent a regression.
- Loading branch information
Showing
7 changed files
with
106 additions
and
50 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
packages/react-dom/src/__tests__/ReactDOMNestedEvents-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('ReactDOMNestedEvents', () => { | ||
let React; | ||
let ReactDOM; | ||
let Scheduler; | ||
let TestUtils; | ||
let act; | ||
let useState; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
Scheduler = require('scheduler'); | ||
TestUtils = require('react-dom/test-utils'); | ||
act = TestUtils.unstable_concurrentAct; | ||
useState = React.useState; | ||
}); | ||
|
||
// @gate experimental | ||
test('nested event dispatches should not cause updates to flush', async () => { | ||
const buttonRef = React.createRef(null); | ||
function App() { | ||
const [isClicked, setIsClicked] = useState(false); | ||
const [isFocused, setIsFocused] = useState(false); | ||
const onClick = () => { | ||
setIsClicked(true); | ||
const el = buttonRef.current; | ||
el.focus(); | ||
// The update triggered by the focus event should not have flushed yet. | ||
// Nor the click update. They would have if we had wrapped the focus | ||
// call in `flushSync`, though. | ||
Scheduler.unstable_yieldValue( | ||
'Value right after focus call: ' + el.innerHTML, | ||
); | ||
}; | ||
const onFocus = () => { | ||
setIsFocused(true); | ||
}; | ||
return ( | ||
<> | ||
<button ref={buttonRef} onFocus={onFocus} onClick={onClick}> | ||
{`Clicked: ${isClicked}, Focused: ${isFocused}`} | ||
</button> | ||
</> | ||
); | ||
} | ||
|
||
const container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
const root = ReactDOM.unstable_createRoot(container); | ||
|
||
await act(async () => { | ||
root.render(<App />); | ||
}); | ||
expect(buttonRef.current.innerHTML).toEqual( | ||
'Clicked: false, Focused: false', | ||
); | ||
|
||
await act(async () => { | ||
buttonRef.current.click(); | ||
}); | ||
expect(Scheduler).toHaveYielded([ | ||
'Value right after focus call: Clicked: false, Focused: false', | ||
]); | ||
expect(buttonRef.current.innerHTML).toEqual('Clicked: true, Focused: true'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters