问题描述
-我想做什么-
我希望通过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 (将#修改为@)