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

[Migrating to modern redux] Possible Unhandled Promise Rejection TypeError: Object is not a constructor #3241

Closed
Saad-Bashar opened this issue Mar 8, 2023 · 8 comments

Comments

@Saad-Bashar
Copy link

Hi, recently I have been trying to migrate our old redux to the redux toolkit in our react native app. I successfully created a separate branch with the redux toolkit working correctly. However, suddenly after a few days, when I returned to this branch my app started to throw this error Possible Unhandled Promise Rejection: TypeError: Object is not a constructor I confirmed it to be related to confgureStore from redux-toolkit. When I am not using it I do not get this error. Whatever I try to do now I always get this error message. I tried to reset yarn, metro server, etc.

This is my store setup,

let persistor = null;
const sagaMiddleware = createSagaMiddleware();
const config = {
  key: 'root',
  storage: AsyncStorage,
  transforms: [
    immutableTransform({
      whitelist: ['user', 'notification', 'shifts', 'settings'],
    }),
  ],
  whitelist: ['user', 'notification', 'shifts', 'settings'],
};

export default function configureStore() {
  const args = [sagaMiddleware].filter(Boolean);
  const reducers = persistReducer(config, rootReducer());
  const enhancers = [applyMiddleware(...args)];
  const persistConfig = {enhancers};

  const store = toolkitConfigureStore({
    reducer: reducers,
    middleware: getDefaultMiddleware =>
      getDefaultMiddleware({
        immutableCheck: false,
        serializableCheck: false,
      }).concat(api.middleware),
    enhancers,
  });
  persistor = persistStore(store, persistConfig, () => {});
  sagaMiddleware.run(rootSaga);

  setupListeners(store.dispatch);
  return {store, persistor};
}

and in App.js, I have the following,

let {store, persistor} = configureStore();
const Root = () => {
  return (
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <AppWithNavigationStates />
        </PersistGate>
      </Provider>
  );
};

Any suggestion on this? Thanks a lot!

@phryneas
Copy link
Member

phryneas commented Mar 8, 2023

That error message should also contain a stack trace, pointing at a specific line in your code. Could you add that?

@phryneas
Copy link
Member

phryneas commented Mar 8, 2023

Generally though, you should .concat your sagaMiddleware to the middlewares, not add another applyMiddleware enhancer. (Please also note that going to the future we recommend not using sagas for new code.)

@Saad-Bashar
Copy link
Author

Unfortunately react native is not helping here it seems :(

Simulator Screen Shot - iPhone 13 - 2023-03-08 at 13 55 39

Totally agree about the saga. As this project was using saga, we will need to remove all the saga with RTKQ eventually but for now we are stuck.

@Saad-Bashar
Copy link
Author

This is another screenshot of the error message,

Simulator Screen Shot - iPhone 13 - 2023-03-08 at 13 58 10

@phryneas
Copy link
Member

phryneas commented Mar 8, 2023

Unfortunately, that makes it very difficult to help. All I can suggest is that you do your store setup step by step. Here you are adding an api, the saga middleware, listeners and whatnot all at once. Do one thing after the next. Once that doesn't give you an error, do the next step.

Blank state.
Blank state with saga.
Blank state with saga and persisted reducer.
Add the api.
Etc.

@Saad-Bashar
Copy link
Author

I agree, thanks a lot! I was trying to follow this as well. Will continue to do so. Just a question, I have the plain redux installed as well besides the rtk. Will that cause any issue?

@phryneas
Copy link
Member

phryneas commented Mar 8, 2023

You don't need it, since plain Redux is part of RTK. But it probably won't cause any problems either.

@Saad-Bashar
Copy link
Author

I managed to fix that issue, turns out I had to do some cleanups. Thanks a lot, @phryneas for your quick response. You are amazing!

yarn cache clean
rm -rf node-modules
rm -rf yarn.lock
yarn

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

2 participants