【问题标题】:react native reselect state is undefined反应本机重新选择状态未定义
【发布时间】:2021-08-17 15:40:44
【问题描述】:

为什么会出现此错误:

undefined 不是一个对象。 state.shopping_cart

重新选择

import { createSelector } from 'reselect';

const carts = state => state.shopping_cart.cart;

export const allCarts = createSelector(
  carts,
  (data) => data
);

购物车:

import { createSlice } from "@reduxjs/toolkit";
import _ from 'lodash';

const shopping_cart = createSlice({
    name: 'shopping_cart',
    initialState: {
      cart: []
    },
....
});

export const { addCart, removeCart, removeCartByProductID, addAmountOnCartItem } = shopping_cart.actions;
export default shopping_cart.reducer;

我是这样打电话的:

allCarts();

【问题讨论】:

    标签: react-native redux expo reselect


    【解决方案1】:

    您应该将状态传递给选择器allCarts(state);

    您可以通过以下方式访问状态:

    import { useSelector } from 'react-redux'
    
    const selectedData = useSelector(state => state);
    

    【讨论】:

    • 但我在哪里得到状态?
    • 这取决于你是否在一个组件中,或者你是否使用了钩子。要回答有关如何访问状态 ich 的问题,只需要更多信息。 1. 你想访问组件中的状态吗? 2. 你用钩子吗? 3. 店铺如何提供?
    • 我将它与钩子一起使用,是的,我想访问组件中的状态
    猜你喜欢
    • 2018-08-09
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-07-20
    • 2021-03-24
    • 2020-08-20
    相关资源
    最近更新 更多