Skip to content
Merged
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion content/docs/reference-react-dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The `react-dom` package also provides modules specific to client and server apps

The `react-dom` package exports these methods:
- [`createPortal()`](#createportal)

- [`flushSync()`](#flushsync)

These `react-dom` methods are also exported, but are considered legacy:
- [`render()`](#render)
Expand Down Expand Up @@ -55,6 +55,21 @@ createPortal(child, container)
```

Creates a portal. Portals provide a way to [render children into a DOM node that exists outside the hierarchy of the DOM component](/docs/portals.html).

### `flushSync()` {#flushsync}

```javascript
flushSync(callback)
```

Force React to flush updates synchronously. This method is useful for being able to read the value of `state` immediately.
Copy link
Contributor

Choose a reason for hiding this comment

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

This a bit misleading since the easiest way to read the value of state immediately is just read what you set it to. I don't think we should encourage this as a workaround for that.

It's more about reading the outcomes. Almost always from the DOM.

It could use an explanation what the purpose of the callback is since callbacks are typically associated with async operation completing, which this by definition is not.

It's meant to form a grouping of any setStates that you need to flush synchronously.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, I was being a little value about the callback because it's not necessarily only the updates passed in that are flushed, correct? I.e. just calling flushSync() without a callback will flush work?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, if you happen to know that they're also scheduled in the sync lane. You can force any pending discrete ones to flush.

However, it's a little sketchy to assume that the context you're executing within is already discrete so adding more setStates before it might not be a good idea. Since you could've been wrapped in a startTransition.

// Reasonable if that's your use case
flushSync();
// Bad
setState(true);
flushSync();
// Good
flushSync(() => setState(true));

Copy link
Member Author

@rickhanlonii rickhanlonii Mar 19, 2022

Choose a reason for hiding this comment

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

I'm trying to avoid explaining discrete updates since we haven't really explained it elsewhere, how about a note like:

flushSync may also flush updates outside of the callback when necessary to flush the updates provided. For example, if there are pending updates from a click, React may flush those before flushing the updates inside the callback.

Copy link
Contributor

@sebmarkbage sebmarkbage Mar 19, 2022

Choose a reason for hiding this comment

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

Sounds good but that part might even need explanation.

The important part is that you put the updates you want to do inside the function.


> Note:
>
> `flushSync` can have a significant impact on performance. Use sparingly.
>
> `flushSync` may also run pending effects and synchronously apply any updates they contain before returning.

## Legacy Reference
### `render()` {#render}
```javascript
Expand Down