簡體   English   中英

反應 state 突變問題

[英]react state mutation issue

我在 React 上做了一個購物車,但我在減速器 function 上有這條線:

state.cartItems.push({
      ...action.payload,
      quantity: 1,
    });

前幾行會導致不良行為。

我的理解是不能對 state 進行突變,所以我試試這個:

state.cartItems.concat({
      ...action.payload,
      quantity: 1,
    });

並且不工作

我從 Alex Banks 和 Eve Porcello 的 Learning React 2 中得到了這個想法,展示了這個例子:

let list = [{ title: "Rad Red" }, { title: "Lawn" }, { title: "Party Pink" }];

const addColor = (title, array) => array.concat({ title });

console.log(addColor("Glam Green", list).length); // 4
console.log(list.length); // 3”

雖然這會起作用,但什么也不做

有關該應用程序的更多實時版本,請點擊此處

以及此處的特定文件 go

我在 React 上做了一個購物車,但我在減速器 function 上有這條線:

state.cartItems.push({
      ...action.payload,
      quantity: 1,
    });

前幾行會導致不良行為。

我的理解是不能對 state 進行突變,所以我試試這個:

state.cartItems.concat({
      ...action.payload,
      quantity: 1,
    });

並且不工作

我從 Alex Banks 和 Eve Porcello 的 Learning React 2 中得到了這個想法,展示了這個例子:

let list = [{ title: "Rad Red" }, { title: "Lawn" }, { title: "Party Pink" }];

const addColor = (title, array) => array.concat({ title });

console.log(addColor("Glam Green", list).length); // 4
console.log(list.length); // 3”

雖然這會起作用,但什么也不做

有關該應用程序的更多實時版本,請點擊此處

以及此處的特定文件 go

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM