Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does flushSync do in react?

Tags:

reactjs

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?

like image 588
Mohamad Shiralizadeh Avatar asked Jul 04 '20 06:07

Mohamad Shiralizadeh


People also ask

What is the purpose of render 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.

What is hydration in React?

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.

What is virtual DOM and how it works?

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 used for write an example?

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.


1 Answers

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

like image 168
Chilarai Avatar answered Sep 25 '22 07:09

Chilarai