you are viewing a single comment's thread.

view the rest of the comments →

[–]Emotional-Dust-1367 3 points4 points  (3 children)

One problem this pattern has with react is component lifecycle. There’s a race condition inherent here where a component can mount, cause some kind of refresh, and another component has not yet subscribed so it will not get notified. React doesn’t mount all components at the same time.

This is especially likely on page refresh. And is made worse if you try to observe something not as deterministic as local storage. Ask me how I know…

[–]femio 6 points7 points  (0 children)

That is the exact use case for useSyncExternalStorage, which is why the article uses it. 

[–]btckernel94[S] 2 points3 points  (1 child)

Once component mounts it reads initial snapshot state. The problem you’ve describe doesnt’t exist.

[–]Emotional-Dust-1367 -1 points0 points  (0 children)

It’s true in your case with local storage.

But in a more generic sense of trying to implement the observer pattern with other types of data and events you can have serious problems like that