react-redux: Redux Toolkit Query 2.0 Beta Uncaught Error: could not find react-redux context value

What version of React, ReactDOM/React Native, Redux, and React Redux are you using?

  • React: 18.2.0
  • ReactDOM/React Native: 18.2.0
  • Redux: 5.0.0-beta.0
  • Redux Toolkit: 2.0.0-beta.0
  • React Redux: 8.0.7

What is the current behavior?

When upgrading from react-redux 8.0.5 to 8.0.7, along side of Redux Toolkit 2.0.0-beta.0 + Redux 5.0.0-beta.0, my application code throws an error when using an RTK Query hook:

Uncaught Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>
    at useReduxContext (useReduxContext.js:24:11)
    at useStore2 (useStore.js:17:9)
    at useDispatch2 (useDispatch.js:14:19)
    at useQuerySubscription (buildHooks.ts:689:24)
    at useQuery (buildHooks.ts:964:42)
    at RequireAuthentication (RequireAuthentication.tsx:34:7)

The application code is wrapped in a Provider and has been functioning normally for months with the Redux Toolkit alphas, and is also functioning normally with the just-released Redux Toolkit beta. It is only when I upgrade to react-redux 8.0.7 that the error is thrown. When I downgrade to react-redux 8.0.5, without modifying any other dependencies, the error is not thrown.

Interestingly, useSelector(), which I would assume is accessing the same exact context from the provider, works fine. But useGet*Query() from RTK Query throws. Final note, I am using Yarn PnP and Yarn Workspaces.

What is the expected behavior?

No error should be thrown, as the component is wrapped in a provider.

Which browser and OS are affected by this issue?

Chrome 113 on macOS Ventura

Did this work in previous versions of React Redux?

  • Yes

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 25 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I’ll see if I can get out 8.1.1 tonight with the context fix and the peerDep removal

@shermify going forward, we should be able to solve that “singleton context item” problem with #2039.

Works for me. Thanks for being on top of this so quickly!

@markerikson, the dependency hashes with v8.1.1, they’re beautiful 🥲 Fixed for both RTK and react-redux. Oh and also all of the behavior is seeming back to normal. I clicked around a bunch in different apps and saw no occurrences of missing context. Thanks all! I even learned some new things through this.

react-redux-hashes

okay, 8.1.1 is out:

https://github.com/reduxjs/react-redux/releases/tag/v8.1.1

Please let us know if that fixes things!

it’s an optional peer dependency because it’s entirely possible to use RTK without react-redux - it’s just the react specific entry points that would need it, basically

@shermify thank you for the info and repro. I did some more local testing over the past few days and am seeing the exact same thing as you (single installed dependencies, but multiple virtual instances of both RTK and react-redux). I tried tweaking lots of things, but always got the same outcome. I think your theory sounds correct:

I also have the same feature use case - a monorepo (Yarn Workspaces) that has multiple apps that connect to common APIs. The core workspace defines the shared slices, which are then imported and consumed by multiple other app workspaces. This setup has been working great up until now (and I’ve been using the RTK alpha for many months).

It feels reasonable that the peer dependency would be declared in either RTK or react-redux, but not both. And I’m not an expert here, but it actually seems to make more sense to remove the peer dependency from RTK instead, since that’s the “larger/core/foundational” library (it’s my understanding that you can technically use RTK without react-redux, but you can’t use react-redux without react and redux or RTK). For example:

  • react does not declare react-dom as a peer dependency (but react-dom does declare react as a peer dependancy)
  • redux does not declare react-redux or @reduxjs/toolkit as peer dependencies
  • react-redux declares 6 optional peer dependencies. 5 of those optional peer dependencies do not list react-redux as their peer dependency. @reduxjs/toolkit is the only peer dependency in that list to have a circular peer dependency declared.

@markerikson @phryneas again appreciate the responsiveness and discussion!

Thanks so much for the responsiveness @EskiMojo14 and @markerikson. I’m about to sign off for the weekend, but will try to create something useful to help debug next week. FYI this isn’t actively blocking, everything works 100% if I stay on react-redux 8.0.5 even when I update everything else to latest including alpha and beta versions. I opened the issue just as a proactive measure to sniff out a potential issue with the beta.