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

configureStore type problem #1002

Closed
AHBruns opened this issue Apr 16, 2021 · 13 comments
Closed

configureStore type problem #1002

AHBruns opened this issue Apr 16, 2021 · 13 comments

Comments

@AHBruns
Copy link

AHBruns commented Apr 16, 2021

So, I've been following the guides and am trying to dispatch an async thunk from another async thunk. To do this, the docs say I need to do:

// store.ts
import { sessionReducer } from "./slices/session";
import { userReducer } from "./slices/user";
export * as session from "./slices/session";
export * as user from "./slices/user";
export * as userThunks from "./thunks/user";
export * as sessionThunks from "./thunks/session";

export const store = configureStore({
  reducer: {
    session: sessionReducer,
    user: userReducer,
  },
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

Then AppDispatch should include type info for a dispatch that works with thunks. In theory I could then do something like the following:

// ./thunks/login.ts
import { createAsyncThunk } from "@reduxjs/toolkit";
import { AppDispatch, sessionThunks } from "../store";

export const login = createAsyncThunk<
  boolean,
  { email: string; password: string },
  {
    dispatch: AppDispatch;
    rejectValue: "WRONG_EMAIL_OR_PASSWORD" | "UNKNOWN";
  }
>("login", (body, thunkAPI) => {
  thunkAPI.dispatch(sessionThunks.loadSession(body));
  return true;
});

Unfortunately, this doesn't work. I get the following error:

Screen Shot 2021-04-16 at 12 46 11 PM

In fact, AppDispatch doesn't even seem to know anything about thunks.

Screen Shot 2021-04-16 at 12 43 28 PM

So, I guess my question is: Why isn't configureStore working as I'm expecting?

@AHBruns
Copy link
Author

AHBruns commented Apr 16, 2021

Figured it out. Needed to manually install redux (not sure how I got this far without it, tbh).

@AHBruns AHBruns closed this as completed Apr 16, 2021
@markerikson
Copy link
Collaborator

markerikson commented Apr 16, 2021

@AHBruns That doesn't sound right. RTK already depends on the redux package, and in fact re-exports all types and APIs from the Redux core. You shouldn't have had to import or install anything else.

The code you're showing looks correct, so I'm not sure why there would be any issues here.

@AHBruns
Copy link
Author

AHBruns commented Apr 16, 2021

@markerikson Yeah, I'm not sure why, but the issue was definitely solved by installing redux. If it helps, I'm using yarn 2 which has all sorts of pitfalls right now. So, this might be a package manager specific thing.

@ChiaJune
Copy link

ChiaJune commented Apr 17, 2021

same here.
@reduxjs/toolkit version: 1.6.0-alpha.0

Although the @reduxjs/toolkit version is 1.6.0-alpha.0, but it's dependence redux is still 4.0.5

@markerikson
Copy link
Collaborator

Can someone put together a project that reproduces this issue? If this is specifically a problem with the alpha, I need to see this so we can fix it.

Additionally, RTK 1.6.0-alpha.0 should depend on redux@^4.1.0-alpha.0.

@markerikson markerikson reopened this Apr 17, 2021
@ChiaJune
Copy link

I tried to reproduce the problem in codesandbox, but everything worked fine.
When I tested it locally, I found this problem seemed to have something to do with @types/react-redux.
I found @types/react-redux was automatically installed when I install react-redux.
When I remove @types/react-redux from devDependencies, everything will be fine.

@markerikson
Copy link
Collaborator

Yes, as of React-Redux 7.2.3, we now specifically auto-install @types/react-redux as a dependency so that people don't forget to install it. Not sure how that would have any effect on something to do with the Redux core and RTK, though.

@jabrks
Copy link

jabrks commented May 7, 2021

I was also seeing this error after installing the alpha. It seemed to be caused by the presence of an older version of Redux core. I managed to fix it by re-installing React-Redux.

✅ Working

npm ls redux

├─┬ @reduxjs/toolkit@1.5.1
│ └── redux@4.0.5 
└─┬ react-redux@7.2.3
  └─┬ @types/react-redux@7.1.16
    └── redux@4.0.5  deduped

❌ Broken (after installing the RTK alpha)

npm ls redux

├─┬ @reduxjs/toolkit@1.6.0-alpha.2
│ └── redux@4.1.0 
└─┬ react-redux@7.2.3
  └─┬ @types/react-redux@7.1.16
    └── redux@4.0.5

✅ Working (after re-installing React-Redux)

npm ls redux

├─┬ @reduxjs/toolkit@1.6.0-alpha.2
│ └── redux@4.1.0 
└─┬ react-redux@7.2.3
  └─┬ @types/react-redux@7.1.16
    └── redux@4.1.0 

@markerikson
Copy link
Collaborator

Hmm. Based on that, I'm not sure there's anything specific we can do to fix this sort of issue.

@types/react-redux depends on redux@^4.0.0. So, redux@4.1.0 qualifies, but NPM doesn't seem to have deduped things there.

@markerikson
Copy link
Collaborator

Listed this in the 1.6 release notes as a potential issue.

@makirby
Copy link

makirby commented Jun 8, 2021

Just chiming in to say I ran into the exact same issue today. Gave me a few hours of headscratching.

Listed this in the 1.6 release notes as a potential issue.

Wish I'd read this yesterday. Updating react-redux sorted it out for me

@ryuker16
Copy link

ryuker16 commented Jul 1, 2021

I ran into this exact same issue but totally pre-loaded states on configure store.

The types for preLoadedState parameters in configureStore have changed from DeepPartial to PreLoadedState type which is much more strict on what it allows for the param(1:1 mapping now required). I wouldn't be surprised if the other types in configureStore has changed in most recent update.

@phryneas
Copy link
Member

phryneas commented Jul 1, 2021

It was pretty much a bug on type level before - you can skip a single reducer's state, but giving a reducer "half" of it's preloaded state will not merge it with the initialState, but leave the other half undefined, leading to all kinds of possible bugs.

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