如何在redux addcasus满足的情况下分配数组

发布于 2025-02-11 04:33:01 字数 1622 浏览 2 评论 0原文

我遇到了一个问题,试图弄清楚如何在异步提取后如何分配一系列用户。我有一个简单的接口用户,它具有ID和一个名称:

export interface User {
  id: number
}

然后,我有一个包括一系列用户的Userstate:

export interface UserState {
  loading: boolean
  users: User[] 
  status: userStatus
  error: string | null | undefined
}

在我的切片中,我有一个和同步的功能:

export const getUsers = createAsyncThunk(
  'user/fetchUsers',
  async (users: []) => {
      const response = await fetchUsers();
      return response.data;
  }
);

我没有还原器,但我确实有外来库:

const userSlice = createSlice({
    name: 'user',
    initialState,
    reducers: {},
    extraReducers: builder => {
        builder.addCase(getUsers.fulfilled, (state, {payload: users}) => {
            state.loading = false
            state.status = 'idle'
            state.users = users  //??? Or something like this...???
            state.error = ''
        })
    }        
})

为了清楚起见,我只是在其中包括了一个AddCase,我试图将JSON格式的预期用户数组分配到用户数组中。我尝试了许多不同的语法变化,并且遇到了类似的错误:

类型'用户[]'不能分配给类型'(()=> element)[]'。 类型“用户”不可分配给type'()=>元素'。 键入“用户”不提供签名的匹配'():element'.ts(2322)

我不确定如何解决这个问题。我研究了许多教程和示例,但是有许多不同的做事方式。我不确定我是否接近解决方案 - 可以从我的位置进行简单而直接的修复 - 或者我远离解决这个问题的世界。我觉得自己好像跌倒了太多的兔子孔,最终创造了一个不太适合的混合部件的怪物。

感谢您的帮助!

I am having a problem trying to figure out how assign an array of users after an async fetch. I have a simple interface User, which has an id and a name:

export interface User {
  id: number
}

I then have a UserState that includes an array of users:

export interface UserState {
  loading: boolean
  users: User[] 
  status: userStatus
  error: string | null | undefined
}

In my slice, I have an asychronous function:

export const getUsers = createAsyncThunk(
  'user/fetchUsers',
  async (users: []) => {
      const response = await fetchUsers();
      return response.data;
  }
);

I don't have reducers but I do have extraReducers:

const userSlice = createSlice({
    name: 'user',
    initialState,
    reducers: {},
    extraReducers: builder => {
        builder.addCase(getUsers.fulfilled, (state, {payload: users}) => {
            state.loading = false
            state.status = 'idle'
            state.users = users  //??? Or something like this...???
            state.error = ''
        })
    }        
})

For clarity, I've just included this one addCase where I am trying to assign the expected array of users in JSON format into the users array. I've tried a number of different syntax variations and I'm getting errors like:

Type 'User[]' is not assignable to type '(() => Element)[]'.
Type 'User' is not assignable to type '() => Element'.
Type 'User' provides no match for the signature '(): Element'.ts(2322)

I'm not entirely sure how to address this one. I've looked at a number of tutorials and examples but there are many different ways of doing things. I'm not sure if I'm close to a solution -- there is a simple and direct fix from where I am -- or if I'm a world away from fixing this. I feel as if I've gone down too many rabbit holes and ended up creating a monster of mixed parts that don't quite fit together.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2025-02-18 04:33:01

从错误消息中,似乎您正在尝试将组件类型分配给用户接口 - 似乎您在某个时候正在导入user component您的用户接口。通过进口并找到错误的导入将解决您的问题。

From the error message, it seems as if you are trying to assign a component type to the User interface here - it seems you have at some point been importing a User component instead of your User interface. Going through your imports and finding the wrong import will fix your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文