如何在redux addcasus满足的情况下分配数组
我遇到了一个问题,试图弄清楚如何在异步提取后如何分配一系列用户。我有一个简单的接口用户,它具有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从错误消息中,似乎您正在尝试将组件类型分配给
用户
接口 - 似乎您在某个时候正在导入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 aUser
component instead of yourUser
interface. Going through your imports and finding the wrong import will fix your problem.