简体   繁体   English

Redux 工具包打字稿——我的 PayloadAction 类型是什么?

[英]Redux toolkit typescript -- what is my PayloadAction type?

I'm trying to write a slice for RTK and I'm not sure what the type of my payload should be.我正在尝试为 RTK 编写一个切片,但我不确定我的有效负载的类型应该是什么。 My state -- InviteeState is an array of people objects我的状态InviteeState是一组人员对象

interface InviteeState {
  people: {
    name: string,
    age: number,
    url: string,
    note?: string
  }[]
}

Then I'm setting my initialState然后我设置我的initialState

const initialState: InviteeState = {
  people: [], 
}

And in my slice:在我的切片中:

export const inviteesSlice = createSlice({
  name: "invitees",
  initialState,
  reducers: {
    // method to update state
    addInvitee: (state, action: PayloadAction<"people"[]>) => {
        state.people.push(action.payload)
    }
  }
})

But In the state.people.push(action.payload) line the action.payload is throwing the error但是在state.people.push(action.payload)行中action.payload抛出错误

"Type '"people"[]' is missing the following properties from type 'WritableDraft<{ name: string; age: number; url: string; note?: string | undefined; }>': name, age, url" “类型 '”people”[]' 缺少类型 'WritableDraft<{ name: string; age: number; url: string; note?: string | undefined; }>': name, age, url 中的以下属性”

I'm not sure if my PayloadAction<"people"[]> type is wrong or if my inistialState is wrong also.我不确定我的PayloadAction<"people"[]>类型是否错误,或者我的inistialState是否也错误。

How do I know what the correct type should be?我怎么知道正确的类型应该是什么?

Your payloadAction should be person object.您的 payloadAction 应该是 person 对象。

First declare the type for "people":首先声明“people”的类型:

interface Person {
    // properties
}

interface InviteeState {
  people: Person[]
}

Then the initial state would be:那么初始状态将是:

const initialState: InviteeState = {
  people: Person[] = [], 
}

You can use Person in your slice:您可以在切片中使用Person

export const inviteesSlice = createSlice({
  name: "invitees",
  initialState,
  reducers: {
    // adding Person to state tree
    addInvitee: (state, action: PayloadAction<Person>) => {
        state.people.push(action.payload)
    }
  }
})

You can call that action like:您可以调用该操作,例如:

let person:Person // of course with actual data in this

dispatch(
    addInvitee(people)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 redux-toolkit 中正确使用带有元类型的 PayloadAction? - How to properly use PayloadAction with meta type in redux-toolkit? redux-toolkit 中 createAsyncThunk 中的操作类型是什么? - what is type of action in createAsyncThunk in redux-toolkit? Typescript 和 redux 工具包 - Typescript and redux toolkit 如何检查我的反应应用程序中当前登录的用户类型? 使用 redux 工具包进行我的 state 管理 - How to check what type of user is currently logged in my react app? Using redux toolkit for my state managment Typescript 类型的 PayloadAction - Typescript types of PayloadAction 是否可以对 Redux Toolkit Slice 的状态使用 typescript Union 类型? - Is is possible to use an typescript Union type for the state of a Redux Toolkit Slice? 在 Redux Toolkit 中,TypeScript 不会捕获错误的状态类型 - In Redux Toolkit, TypeScript doesn't catch the wrong type of state Redux-toolkit 中异步操作的打字稿等价物是什么 - What is the typescript equivalent for Asynchronous action in Redux-toolkit Redux-toolkit - extraReducer 中出现类型错误的原因可能是什么? - Redux-toolkit - What could be the reason for a type-error in an extraReducer? Redux 工具包 typescript 类型问题与 next js 和 next-redux-wrapper - Redux toolkit typescript type issue with next js and next-redux-wrapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM