Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "Tearing" in the context of the React-Redux?

Version 6.0 of React-Redux mentions:

In version 6, all components read the same current store state value from context, which means the tree will be consistent and not have "tearing".

I get that this is beneficial, but I'd like to understand the meaning of "tearing" in this context better, and I'd like to understand how the new approach they outline actually reduces "tearing," if anyone can elaborate.

like image 518
Slbox Avatar asked Feb 26 '19 18:02

Slbox


People also ask

What is Redux in React in simple words?

What is Redux? Redux is a predictable state container designed to help you write JavaScript apps that behave consistently across client, server, and native environments, and are easy to test. While it's mostly used as a state management tool with React, you can use it with any other JavaScript framework or library.

What is react Redux used for?

React Redux is the official React binding for Redux. It allows React components to read data from a Redux Store, and dispatch Actions to the Store to update data. Redux helps apps to scale by providing a sensible way to manage state through a unidirectional data flow model.

What is useSyncExternalStore?

useSyncExternalStore is a hook recommended for reading and subscribing from external data sources in a way that's compatible with concurrent rendering features like selective hydration and time slicing. const state = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot?); PrevioususeState NextuseTransition.

Does Redux support React 18?

React-Redux v8 is still compatible with all versions of React that have hooks (16.8+, 17. x, and 18. x; React Native 0.59+), and should just work out of the box. In most cases, it's very likely that the only change you will need to make is bumping the package version to "react-redux": "^8.0" .


1 Answers

I'm a Redux maintainer, and I wrote that paragraph.

This is specifically a concern that has been raised by Andrew Clark from the React team as a potential issue with external state management tools when used with React's upcoming "Concurrent Mode".

In Concurrent Mode, React will be able to pause a render pass through the tree, and resume calculating the rest of the tree later.

If the components in the tree are reading an external value, and that value were to change while React's rendering is paused, then some of the upper components in the tree might have rendered using external value 1, and some of the later components might have rendered using external value 2. That would result in inconsistent render output, because different parts of the tree determined their behavior based on differing values in the same render pass. This is "tearing".

Part of the idea behind using createContext for v6 was that since React ensures a given render pass uses the same context value everywhere, there would be no chance of tearing.

The v6 implementation does work, but it's not as efficient in some cases as we'd hoped. We're currently working on coming up with a different internal implementation that goes back to using direct subscriptions instead. This does potentially mean that tearing is a possibility again, but at this point we need to sit back and wait for the React team to finish putting Concurrent Mode together before we can spend time seeing what the issues really are.

like image 132
markerikson Avatar answered Oct 10 '22 23:10

markerikson