React native 应用程序重新启动后,购物车项目未显示在购物车中

React native 应用程序重新启动后,购物车项目未显示在购物车中,react-native,redux,react-redux,shopping-cart,asyncstorage,React Native,Redux,React Redux,Shopping Cart,Asyncstorage,应用程序重新启动后,购物车项目不会显示在购物车中。我认为这是由于空数组cartItems造成的。 似乎this.props.cartItems是未定义的,这就是为什么在它们上尝试使用Array.prototype.slice时失败的原因 问题似乎起源于mapStateToProps,您正在引用state.cart.items,但在您的reducer中,结构似乎是state.items 因此,这应该起作用: const-mapStateToProps=(state)=>({ 产品:state.pr

应用程序重新启动后,购物车项目不会显示在购物车中。我认为这是由于空数组cartItems造成的。


似乎
this.props.cartItems
未定义的
,这就是为什么在它们上尝试使用
Array.prototype.slice
时失败的原因

问题似乎起源于
mapStateToProps
,您正在引用
state.cart.items
,但在您的reducer中,结构似乎是
state.items

因此,这应该起作用:

const-mapStateToProps=(state)=>({
产品:state.products.filteredItems,
cartItems:state.items,
});
         import {AsyncStorage} from 'react-native'
        const cartItems = [] // 
         AsyncStorage.getItem("cartItems").then(res=>
           res!=null?res:[])
           
         const initState = { cart: { items: cartItems } };
        const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
       const store = createStore(
           rootReducers,
           initState,
           composeEnhancer(applyMiddleware(thunk))
         );
         export default store;