Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigation: try React-Redux v7 / v8 with React 18 alpha #1732

Closed
markerikson opened this issue Jun 10, 2021 · 12 comments
Closed

Investigation: try React-Redux v7 / v8 with React 18 alpha #1732

markerikson opened this issue Jun 10, 2021 · 12 comments
Milestone

Comments

@markerikson
Copy link
Contributor

markerikson commented Jun 10, 2021

React 18 is now available as preview alpha releases (per https://reactjs.org/blog/2021/06/08/the-plan-for-react-18.html ).

The React team has put together a Working Group discussion forum to describe the changes in React 18 in more detail: https://github.com/reactwg/react-18/discussions

Looking at that forum, there's several issues that may be relevant to React-Redux in some way:

I'd like to have some folks start trying out React-Redux v7 with React 18 alpha, and report any interesting findings. We could potentially use some of the example apps from the Redux tutorials, and any of the example CodeSandboxes linked in our docs.

You would need to:

  • Switch the React version to one of the React 18 alphas, and update the app to use React.createRoot() per instructions
  • Try out the app
  • Hand-inspect the behavior to see if anything seems unexpected

We're interested in seeing examples and comparisons with both connect and useSelector.

It would also be worth cloning the React-Redux repo, and updating our React devDep to run our tests against the alphas.

The React team has reported that a brief test of a couple Redux-based apps combined with createRoot + StrictMode did not appear to be working correctly - in particular, no re-rendering happening despite actions being dispatched.

It's very possible that Strict Effects running effects callbacks more than once is causing problems with subscriptions.

We'd appreciate any feedback reports!

Please provide this info:

  • Details on the example app used
  • Link to updated source code / CodeSandbox if possible
  • Description of observed behavior, good or bad ("works okay", "clicking button X doesn't update the UI", etc)
@markerikson markerikson pinned this issue Jun 10, 2021
@gaearon
Copy link
Contributor

gaearon commented Jun 10, 2021

Note to folks trying — if you have StrictMode on, please test both with StrictMode and without it. This is to see if some issues only occur in StrictMode. (StrictMode has gotten “stricter.”)

@timdorr
Copy link
Member

timdorr commented Jun 11, 2021

We actually have tests for StrictMode in our suite already, so it should be good to test at least the basics here.

@markerikson
Copy link
Contributor Author

Okay. First issue here is that there's this new "Strict Effects" thing which is enabled if you're using createRoot() + <StrictMode>, which double-runs the effects in the same way that <StrictMode> has traditionally double-run renders. I could easily see that messing up subscriptions.

So, we'd need to figure out how to do a React 18 upgrade for our tests, which I assume would mean something with React Testing Library since that's doing the rendering?

@nickmccurdy
Copy link
Contributor

nickmccurdy commented Jun 11, 2021

If you're asking about Testing Library compatibility, you may want to subscribe to testing-library/react-testing-library#925 (proposed as an alpha).

@ryota-murakami
Copy link

I have a side project that used to React-Redux v7.2.4, I'd like to try it if React 18 alpha working on CRA v4.0.3.

@markerikson
Copy link
Contributor Author

Comment from the React team on plans for helping the community do updates:

reactwg/react-18#56 (comment)

The first step is to explain what work we see needs to be done at a high level, so we're working on a workgroup post that will be a "Concurrent rendering for library maintainers" guide, and documentation for new APIs libraries will use to support concurrent features. Those docs will be a "kick off" for starting library support. After they're published, we'll do a Q&A / support session, and use the working group to document questions, use cases, patterns, design, etc.

I know we're all really excited to start with the work of adding support to libraries, so I'm sorry it's taking so long. We've spent the last couple weeks kicking off the release (which focused on the gradual adoption strategy), and now we're transitioning to working with libraries to support the new concurrent features.

We expect this work to take months, not weeks, similar to how the hooks release went.

@markerikson
Copy link
Contributor Author

Mobx has a PR going that's tracking some similar status checks for React 18. Worth keeping an eye on that:

mobxjs/mobx#3005

@artem-malko
Copy link

artem-malko commented Aug 3, 2021

Hi @markerikson! I have something interesting for you)

I have a repository with react 18 + SSR + redux for UI state. There is an interactive demo — http://158.101.223.0:5000/

As you can see, there is a button "Patch query string". If you'll click this button, the UI state in a redux-store will be updated with new Date().toString() There is a source code. Top part of the page has a component, that has a subscription to the redux-store vis useSelector.

So, if you will open the demo with a query param strict=true, the whole app will be wrapped with component. If there is no strict query param — without it. Click to the button, that changes the query string works perfectly in strict mode and without it. But, it works in production build only.

If you will clone the repository, exec npm i && make dev and open http://localhost:4000?strict=true, you will see, that the UI won't be changed after the button click.

Actually, I can not understand, what is the difference between develop and production build in case of redux/react-redux. May be it is because of react?

I understand, the project is quite big. But I've tried to make it easy to repeat the problem. Other stuff from that repo is not so important, as think)

@gaearon
Copy link
Contributor

gaearon commented Sep 13, 2021

Example of StrictMode + createRoot breaking it, per request from reactwg/react-18#19 (comment): https://codesandbox.io/s/serverless-wood-uouyy?file=/src/index.js

@markerikson
Copy link
Contributor Author

markerikson commented Sep 14, 2021

And copying over some excellent detective work from @Andarist at reactwg/react-18#19 (reply in thread) :

The problem is not that dispatching doesn't work (although you probably didn't mean exactly that) and it's not even because components are not subscribed - because they are. The problem is that the hooked up listener is a "proxy" listener:

function handleChangeWrapper() {
if (subscription.onStateChange) {
subscription.onStateChange()
}
}

which actually forwards notifications to another one that is put as a property on the subscription object. And that property gets "nullified" in the cleanup of the Provider:
subscription.onStateChange = undefined

while not being re-instantiated in the setup because it's only assigned within useMemo here:
subscription.onStateChange = subscription.notifyNestedSubs

So what happens is that Provider gets notified about a store change - it should notify nested subscriptions but it doesn't because of the mentioned issue. And useSelector (and probably any other inner subscribers like connect etc) are never notified about this because they are subscribed to that parent subscription that lives in the Provider here.

TLDR; lack of symmetry between setup/cleanup of an effect in the Provider led to the notification chain being broken.

@markerikson markerikson changed the title Investigation: try React-Redux v7 with React 18 alpha Investigation: try React-Redux v7 / v8 with React 18 alpha Nov 2, 2021
@markerikson markerikson added this to the 8.0 milestone Dec 21, 2021
@serprex
Copy link

serprex commented Apr 2, 2022

https://etg.dek.im https://github.com/serprex/openEtG works okay after updating to react-redux 8.0.0-beta.3 & react 18.0

Code doesn't use any hooks

@markerikson
Copy link
Contributor Author

React-Redux v8 is out, so I think we can call this done :)

@markerikson markerikson unpinned this issue May 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants