Pull to refresh

Comments 4

Не очень понимаю как работает batch update, react делает ререндер в следующем тике? Или на время вызова колбеков делает monkey-patch на всё что может вызваться асинхронно (setTimeout/Promise/etc)?

Отвечает автор статьи:

Когда происходит батчинг, React собирает все обновления стейта в очередь и вызывает их, а ререндер происходит после всех вызовов обновления стейта. Вот здесь подробнее про этот процесс https://gaopinghuang0.github.io/2020/12/21/react-batched-update

Я так понимаю вопрос был про асинхронное обновление. Т.е. "после всех вызовов обновления стейта" неосуществимо. По ссылке вижу:


However, if we use setState() within setTimeout(), async, and Promise, we will not be able to setup ReactDefaultBatchingStrategy.isBatchingUpdates as true. Instead, ReactUpdateQueue.enqueueSetState() is called, which sets isBatchingUpdates to true but calls transaction.perform() directly. In such case, each setState() is handled independently and triggers a re-render.

По сути в статье вроде описано старое поведение (пролистал по диагонали в поисках async поведения).

In React 18, this is no longer the case. Since all of the updates even in setTimeout are batched, React doesn’t render the result of the first setState synchronously—the render occurs during the next browser tick.

Да, в следующем тике.

Sign up to leave a comment.

Articles