I saw Dan Abramov's demo project at JSConf Iceland, but I didn't understand why he used flushSync
in this code:
import { flushSync } from 'react-dom';
debouncedHandleChange = _.debounce(value => {
if (this.state.strategy === 'debounced') {
flushSync(() => {
this.setState({value: value});
});
}
}, 1000);
What does flushSync do in react?
The ReactDOM.render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.
In React, “hydration” is how React “attaches” to existing HTML that was already rendered by React in a server environment. During hydration, React will attempt to attach event listeners to the existing markup and take over rendering the app on the client.
Virtual DOM is nothing but a strategy that React uses to optimize the performance of an application. It provides a mechanism that compares two render trees to know what exactly has changed and only updates what is necessary on the actual DOM. Like React, Vue and some other frameworks also employ this strategy.
What is ReactDOM? ReactDOM is a package that provides DOM specific methods that can be used at the top level of a web app to enable an efficient way of managing DOM elements of the web page. ReactDOM provides the developers with an API containing the following methods and a few more.
flushSync flushes the entire tree and actually forces complete re-rendering for updates that happen inside of a call, so you should use it very sparingly. This way it doesn’t break the guarantee of internal consistency between props, state, and refs.
It is not documented properly yet. Read more here https://github.com/facebook/react/issues/11527
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With