【问题标题】:Javascript object keys and valuesJavascript 对象键和值
【发布时间】:2023-01-09 13:10:23
【问题描述】:

创建切片是 接受 reducer 函数对象的函数,其中钥匙在 reducer 对象中将用于生成字符串操作类型常量,如下所示:

const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: (state) => state + 1,
  },
})

没关系,但我对此感到困惑。 在哪里钥匙价值观这个对象?

reducers: {
    increment(state) {
        state.value++
    },
    decrement(state) {
        state.value--
    },
    incrementByAmount(state, action) {
        state.value += action.payload
    }
}

【问题讨论】:

    标签: javascript redux


    【解决方案1】:

    键是incrementdecrementincrementByAmount,值是函数本身。这些是“减速器”,它们是作用于您的状态的功能。

    它大致相当于这样说,但语法不同

    reducers: {
      increment: function(state) {
        state.value++
      },
      decrement: function(state) {
        state.value--
      },
      incrementByAmount: function(state, action) {
        state.value += action.payload
      }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多