I am using React + TypeScript for development and using Redux for centralised state management - to store the current user logged in.
Here is the snippet of code inside my React component, where I try to access the user state:
I'm not sure why useSelector returned undefined since I have defined an initialState in the userReducer.
Because you defined the root state as
{user}. In other words, theuserReducer's data is attached to the root state asstate.user.So, your selector needs to be
state => state.user.You also have a problem in your store file. You've created another reducer function with
const rootReducer = combineReducers({userReducer}), and then inferred theRootStatetype from that.That is wrong in two ways:
configureStorealready made its own root reducer function from the definition you provided and is using thatRootStatetype is wrong, because it's being inferred from the wrong functionThat's why you're not even getting a TS error here.
Instead, you need to delete the
combineReducersline, and then do:per our TS usage docs: