第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 redux 更新?tīng)顟B(tài)時(shí)推送反應(yīng)的工作

使用 redux 更新?tīng)顟B(tài)時(shí)推送反應(yīng)的工作

慕哥6287543 2021-10-29 13:50:37
以下代碼是我在容器中調(diào)用這些函數(shù)的減速器的代碼const intialState = {  counter: 0,  results: []};const reducer = (state = intialState, action) => {  switch (action.type) {    case "INCREMENT":      return {        ...state,        counter: state.counter + 1      };    case "STORE_RESULT": {      return {        ...state,        results: state.results.push(state.counter)      };    }  }  return state;};export default reducer;我收到以下錯(cuò)誤TypeError: state.results.push is not a functionreducer1)我在我的項(xiàng)目中使用 redux reducer2)我通過(guò)在我的容器中傳遞來(lái)自我的調(diào)度的類型來(lái)更新?tīng)顟B(tài)3)我試圖通過(guò)推送來(lái)更新數(shù)組(我知道它返回長(zhǎng)度)但我想知道它為什么不起作用4)下面的代碼我在javascript中嘗試過(guò)一切正常var a = {b:[]}a.b.push(11)//output1
查看完整描述

3 回答

?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

push返回新length的 mutated array。使用concat,而不是返回一個(gè)新的數(shù)組

results : state.results.concat(item)

讓我們假設(shè)以下代碼

results : state.results.push('foo')

假設(shè) results有一個(gè)lengthof 5,上面的代碼將斷言

results : 5

下次您嘗試push results這樣做時(shí),您的編譯器會(huì)是什么樣子

5.push('foo')


查看完整回答
反對(duì) 回復(fù) 2021-10-29
?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

返回值的.push就是


調(diào)用方法的對(duì)象的新長(zhǎng)度屬性。


添加值以stae.results使用concat或spread 語(yǔ)法:


case "STORE_RESULT": {

  return {

    ...state,

    results: [...state.results, state.counter]

  };

}


查看完整回答
反對(duì) 回復(fù) 2021-10-29
?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

您應(yīng)該在減速器開(kāi)關(guān)中添加默認(rèn)情況。


并使用[...state.results, state.counter]代替state.results.push(state.counter)。


像這樣


const intialState = {

  counter: 0,

  results: []

};


const reducer = (state = intialState, action) => {

  switch (action.type) {

    case "INCREMENT":

      return {

        ...state,

        counter: state.counter + 1

      };


    case "STORE_RESULT": {

      return {

        ...state,

        results: [...state.results, state.counter] // here

      };

    default:

      return state; // here

    }

  }

};


export default reducer;


查看完整回答
反對(duì) 回復(fù) 2021-10-29
  • 3 回答
  • 0 關(guān)注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)