0

this is my code useSelector

const  user  = useSelector<RootStateOrAny, typeof typeRootReducer>((state) => state.user)

in rootReducer

const rootReducer = combineReducers({
    user: userReducer
})


export default rootReducer
export type typeRootReducer =  ReturnType<typeof rootReducer>

initState

const initState = {
    user: null
}

when I trying to console.log(user.user.name) it just give me cannot read property name of null

1 Answer 1

1

Well, your user is - as you wrote yourself - null. So you are calling null.name, which does not work. Nothing wrong with your selector usage per se, just your state contains something that does not match your code.

Generally, to avoid all the type gymnastics you are doing there, it is recommended to create a useAppSelector pre-typed hook, so you can just do const user = useAppSelector(state => state.user) and everything will be typed correctly automatically.

Follow the TypeScript Quick Start tutorial for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.