Redux 工具包中的负载
Payload in Redux Toolkit
谁能给我解释一下 redux-toolkit 中的 payload 是什么?
这个值从何而来?
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
export interface CounterState {
value: number
}
const initialState: CounterState = {
value: 0,
}
export const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => {
state.value += 1
},
decrement: (state) => {
state.value -= 1
},
incrementByAmount: (state, action: PayloadAction<number>) => {
state.value += action.payload
},
},
})
export const { increment, decrement, incrementByAmount } = counterSlice.actions
export default counterSlice.reducer
createAction function. These three fields (payload
, meta
and error
) adhere to the specification of Flux Standard Actions 创建的 action.payload
。
The optional payload
property MAY be any type of value. It represents the payload of the action. Any information about the action that is not the type or status of the action should be part of the payload
field. By convention, if error
is true
, the payload
SHOULD be an error object. This is akin to rejecting a promise with an error object.
谁能给我解释一下 redux-toolkit 中的 payload 是什么?
这个值从何而来?
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
export interface CounterState {
value: number
}
const initialState: CounterState = {
value: 0,
}
export const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => {
state.value += 1
},
decrement: (state) => {
state.value -= 1
},
incrementByAmount: (state, action: PayloadAction<number>) => {
state.value += action.payload
},
},
})
export const { increment, decrement, incrementByAmount } = counterSlice.actions
export default counterSlice.reducer
createAction function. These three fields (payload
, meta
and error
) adhere to the specification of Flux Standard Actions 创建的 action.payload
。
The optional
payload
property MAY be any type of value. It represents the payload of the action. Any information about the action that is not the type or status of the action should be part of thepayload
field. By convention, iferror
istrue
, thepayload
SHOULD be an error object. This is akin to rejecting a promise with an error object.