简体   繁体   中英

TS2339: Property 'quantity' does not exist on type 'DefaultRootState'

I am trying to call the quantity variant from my redux files with this

const quantity = useSelector(state=>state.quantity)

but I get this error TS2339:

My redux files are these

import {createSlice} from "@reduxjs/toolkit"

const cartSlice = createSlice({
    name:"cart",
    initialState:{
        products: [],
        quantity:0,
        total:0,
    },
    reducers:{
        addProduct:(state,action)=>{
            state.quantity +=1;
            state.products.push(action.payload.product);
            state.total += action.payload.price;
        },
    }
});

export const{addProduct} = cartSlice.actions
export default cartSlice.reducer;

and this

import {configureStore} from "@reduxjs/toolkit"
import cartReducer from "./cartRedux"

export default configureStore({
    reducer:{
        cart:cartReducer,
    }
})

Add in your component file this (above your component right after imports):

type RootState = ReturnType<typeof store.getState>;

and then use it like this:

const quantity = useSelector((state: RootState)=>state.quantity)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM