简体   繁体   English

× TypeError: 无法解构 'Object(...)(...)' 的属性 'xxx',因为它未定义

[英]× TypeError: Cannot destructure property 'xxx' of 'Object(...)(...)' as it is undefined

I am learning react redux, but I am facing this problem,我正在学习反应 redux,但我面临这个问题,

when I dispatch the 'remove' action当我发送“删除”操作时

action行动

export const remove = () => ({
    type: REMOVE
})

in reducer inside switch statement在 reducer 里面的 switch 语句中

case REMOVE:
      return console.log("removed");

      break;

And I use it here on the remove button我在删除按钮上使用它

import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { increase, decrease, remove } from "./../actions";

const CartItem = ({ img, title, price, amount }) => {
  const dispatch = useDispatch();

  const removeItem = () => {
    dispatch(remove());
  };

  return (
    <div className="cart-item">
      <img src={img} alt={title} />
      <div>
        <h4>{title}</h4>
        <h4 className="item-price">${price}</h4>
        {/* remove button */}
        <button className="remove-btn" onClick={removeItem}>
          remove
        </button>
      </div>
      <div>
        {/* increase amount */}
        <button className="amount-btn">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
            <path d="M10.707 7.05L10 6.343 4.343 12l1.414 1.414L10 9.172l4.243 4.242L15.657 12z" />
          </svg>
        </button>
        {/* amount */}
        <p className="amount">{amount}</p>
        {/* decrease amount */}
        <button className="amount-btn">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
            <path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z" />
          </svg>
        </button>
      </div>
    </div>
  );
};

export default CartItem;

the action just console logs, when I click the remove button, it logs 'removed' and then I get this error该操作只是控制台日志,当我单击删除按钮时,它会记录“已删除”,然后出现此错误

TypeError: Cannot destructure property 'cart' of 'Object(...)(...)' as it is undefined.
CartContainer
E:/React_Projects/Redux-demo/starter-project-redux-tutorial-cart-master/src/components/CartContainer.js:6
  3 | import CartItem from "./CartItem";
  4 | import { clearCart } from "./../actions";
  5 | const CartContainer = () => {
> 6 |   const { cart, total } = useSelector((state) => state);
  7 |   const dispatch = useDispatch();
  8 |   
  9 |   const handleClick = () => {

I have used the initial state in my reducer file我在我的减速器文件中使用了初始的 state

import { CLEAR_CART, DECREASE, INCREASE, REMOVE } from "./actions";
// items
import cartItems from "./cart-items";

//initial store
const initialStore = {
    cart: cartItems,
    total: 1487.00,
    amount: 5,
  };

const reducer = (state = initialStore, action) => {
  switch (action.type) {

I don't know what the error means.我不知道错误是什么意思。 Can someone explain and show me a solution.有人可以解释并告诉我一个解决方案。

The error is created by this:错误由此产生:

case REMOVE:
    return console.log("removed");

    break;

You are returning nothing, when you need to be returning the state. So after the remove function is called and the state is set to nothing (undefined), functions which select from the state will throw errors because the state no longer exists.当您需要返回 state 时,您什么都不返回。因此,在调用 remove function 并将 state 设置为空(未定义)后,state 中的 select 将抛出错误,因为 8827443874018 不再存在。 That's why an error is thrown here:这就是这里抛出错误的原因:

const { cart, total } = useSelector((state) => state);

The error says that you can't get the property “cart” from state because state is undefined.该错误表明您无法从 state 获取属性“cart”,因为 state 未定义。

You need to change your reducer so that it returns a valid state after every action.您需要更改减速器,以便它在每次操作后返回有效的 state。 Eventually you'll want to actually remove something, but for now this will work:最终你会想要真正删除一些东西,但现在这会起作用:

case REMOVE:
    console.log("removed");
    return state;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 TypeError:无法解构“useAuth(...)”的属性“xxx”,因为它未定义 - Uncaught TypeError: Cannot destructure property 'xxx' of 'useAuth(...)' as it is undefined 类型错误:无法解构“Object(...)(...)”的属性“isLoading”,因为它未定义 - TypeError: Cannot destructure property 'isLoading' of 'Object(...)(...)' as it is undefined TypeError:无法解构“对象(...)(...)”的属性“用户”,因为它未定义 - TypeError: Cannot destructure property 'user' of 'Object(...)(...)' as it is undefined × TypeError:无法解构'Object(...)(...)'的属性'currentUser',因为它是未定义的 - × TypeError: Cannot destructure property 'currentUser' of 'Object(...)(...)' as it is undefined TypeError:无法解构“对象(...)(...)”的属性“setUser”,因为它未定义 - TypeError: Cannot destructure property 'setUser' of 'Object(...)(...)' as it is undefined TypeError:无法解构'Object(...)(...)'的属性'employees',因为它是未定义的 - TypeError: Cannot destructure property 'employees' of 'Object(…)(…)' as it is undefined TypeError:无法解构“Object(...)(...)”的属性“Provider”,因为它未定义 - TypeError: Cannot destructure property 'Provider' of 'Object(...)(...)' as it is undefined TypeError:无法解构''的属性'',因为它是未定义的 - TypeError: Cannot destructure property '' of '' as it is undefined 未处理的拒绝 (TypeError):无法解构“Object(...)(...)”的属性“setUser”,因为它未定义 - Unhandled Rejection (TypeError): Cannot destructure property 'setUser' of 'Object(...)(...)' as it is undefined TypeError:无法解构“未定义”的属性“位置”,因为它未定义 - TypeError: Cannot destructure property 'position' of 'undefined' as it is undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM