Home > Net >  Issue using selector after converting to configureStore Redux Toolkit
Issue using selector after converting to configureStore Redux Toolkit

Time:09-17

I've just converted my reducer from createStore to configure store and now my selectors that I think should be working, don't.

configureStore below

import { Dispatch } from 'react';
import { Action, configureStore } from '@reduxjs/toolkit';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';

import logger from 'redux-logger';

import {
    testReducer,
} from './reducers';

const rootReducer = () => ({
    testReducer
});

export const store = configureStore({
    reducer: rootReducer,
    devTools: process.env.NODE_ENV !== 'production',
    middleware: getDefaultMiddleware => getDefaultMiddleware().concat(logger),
});

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

export type AppDispatch = typeof store.dispatch;

export const useAppDispatch = (): Dispatch<Action<any>> => useDispatch<AppDispatch>();

export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

selector

export const getTest = (state: RootState) => state.testReducer.test;

error

Property 'test' does not exist on type 'ReducerWithInitialState<TestState>'.ts(2339)

The error seems to go away if I add getInitialState() after testReducer. in the selector but that seems incorrect.

CodePudding user response:

That rootReducer you have there is a function that returns an object with reducers - that's wrong. Instead just make it an object.

const rootReducer = {
    testReducer
};
  • Related