1
export default function Cart(props) {
  const dispatch = useDispatch();
  
  const branch = useSelector((state) => get(state, "vendor.currentBranch"));
const orderInput = useSelector((state) => get(state, "order.orderInputs"));

  const [orderResult, setOrderResult] = useState(null);
  let orderItemLength = orderInput.length

  useEffect(() => {
    let payload = {
      branch_uuid: get(branch, "uuid"),
      menu_items: orderInput,
    };
    
    dispatch(calculateOrder(payload))
      .then((result) => {
        setOrderResult(result);
      })
      .catch(() => {});
   
  }, [orderInput]);

  const deleteItem = (index) => {
    remove(orderInput, (oi, i) => index === i);
    dispatch({
      type: ADD_ORDER_INPUT,
      payload: orderInput,
    });
  };

  return (
          <div className={styles.cartbody} id="scrollstyle-4">
            {map(get(orderResult, "orderItems", []), (oi, i) => {
              return (
                <div className={styles.cartProductBox}>
                  <div className={styles.productName}>
                    <p className={styles.textRed}>
                      <span className={styles.qunatity}>
                        {get(oi, "quantity")}
                      </span>
                      {get(oi, "item_name")}
                      <Popconfirm
                        placement="rightBottom"
                        title={"Are you sure you want to remove this item?"}
                        onConfirm={() => {
                          deleteItem(orderInput,i);
                        }}
                        okText="Yes"
                        cancelText="No"
                      >
                        <DeleteOutlined />
                      </Popconfirm>
                    </p>
                    <span className={styles.subItem}>
                      {map(get(oi, "orderItemAddons", []), (oia, i) => {
                        return `${i === 0 ? "" : ","} ${get(
                          oia,
                          "addon_type_name"
                        )} `;
                      })}
                    </span>
                  </div>

                  <div>
                    <div>
                      <span className={styles.qunatity}>
                        $ {round(get(oi, "item_amount"), 2)}
                      </span>
                    </div>
                    <div style={{ marginTop: 10 }}>
                      {get(oi, "orderItemAddons", []).length > 0 && (
                        <span className={styles.addonPrice}>
                          $ {round(get(oi, "addon_amount"), 2)}
                        </span>
                      )}
                    </div>
                  </div>
                </div>
              );
            })}
          </div>
  );
}

这是我的减速器代码

import {
  ADD_SERVICE,
  ADD_ORDER_INPUT,
  ADD_CALCULATED_ORDER,
} from "../../constants/ActionTypes";

const orderReducer = (
  state = { serviceType: null, orderInputs: [] },
  action
) => {
  switch (action.type) {
    case ADD_SERVICE:
      return { ...state, serviceType: action.payload };

    case ADD_ORDER_INPUT:
      return { ...state, orderInputs: action.payload };

    case ADD_CALCULATED_ORDER:
      return { ...state, orderInputs: action.payload };

    default:
      return { ...state };
  }
};

export default orderReducer;

在上面的代码中,当我从另一个组件添加订单项时,useEffect 被触发,但是当我删除 orderItem(如您在deleteItem()函数中看到的)时,useEffect 没有在我的 redux 存储更新时触发,我的 useEffect 触发器依赖项是 OrderInput变量,如图所示在代码中。我还尝试将 useEffect 的依赖项设置为 order 数组的长度OrderInput

请帮助我知道我做错了什么?

4

1 回答 1

1

面对同样的问题并得到解决,感谢 Chris Answer。

提供一些详细说明,希望它可以帮助下一个人:

最初没有工作,当 redux 存储更改时,useEffect 没有触发。

  // Redux: Subscribe to user Store
  const userStateRedux = useSelector((state: RootState) =>state.user);

  useEffect(() => {
      console.log('hello',userStateRedux);
  }, [userStateRedux]);

Redux 需要引用另一个对象来检测更改状态(在你的 reducer 中执行不可变更新)

In your reducer.js

//Mutable Update: Still referencing ur initial State
state.user=action.user;
state.token=action.token;

//Immutable Update: Referencing a new Object <- USE THIS
state={
      user:action.user,
      token:action.token
};
于 2021-11-11T01:27:15.027 回答