如何解决当服务器响应通过Redux-Tookit存储在Redux Store中时,TypeScript类型不起作用
-我想做什么-
我希望通过TypeScript类型将服务器响应和Redux-Toolkit存储在Redux Store中。但是类型似乎不起作用,并且变量是错误的。
-我做了什么-
HTTP请求和发送:
axios.get(`/api/v1/ad_frames/${id}`)
.then(res => {
dispatch(slice.actions.setAdFrame({ adFrame: res.data.data.adFrame })
}).catch(err => {
console.log(err);
})
服务器响应:
{
data: {
adFrame: {
different_variable: 1,},}
类型:
export interface AdFrameState {
adFrame: AdFrame;
};
export interface AdFrame {
correctVariable: number;
}
const initialState: AdFrameState = {
adFrame: {
correctVariable: 0,};
切片:
const slice = createSlice({
name: "adFrame",initialState,reducers: {
setAdFrame: (state: AdFrameState,action: PayloadAction<{ adFrame: AdFrame }>) => {
state.adFrame = action.payload.adFrame;
},}
});
商店(index.ts):
export type AppState = {
adFrame: AdFrameState,};
const store = configureStore({
reducer: combineReducers<AppState | any>({
adFrame: adFrameSlice.reducer,}),});
export default store;
-结果-
Redux Store中的状态:
State
- AdFrame
- different_variable: 1
-所需结果-
Redux Store中的状态:
State
- AdFrame
- correctVariable: 1
我希望有人能帮助我。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。