【发布时间】:2023-01-19 16:36:45
【问题描述】:
interface ListMakerResponse {
result: number;
detail: string;
message: string;
list: Maker[];
}
interface ListMakerRequest {
dynamic: string;
}
type Maker = {
makerId: string;
makerName: string;
};
type OrganazationState = {
status: "idle" | "loading" | "succeeded" | "failed";
error: Error | null;
response: ListMakerResponse;
};
const initialState: OrganazationState = {
status: "idle",
error: null,
response: {} as ListMakerResponse,
};
export const postMakerList = createAsyncThunk(
"list_maker",
async (_, { rejectWithValue }) => {
const response = await http.usePost<ListMakerResponse, ListMakerRequest>(
"http://localhost:8080/list_maker",
{
dynamic: "gravity",
}
);
return response.data;
}
);
export const OrganazationSlice = createSlice({
name: "organazation",
initialState,
reducers: {},
extraReducers(builder) {
builder.addCase(postMakerList.fulfilled, (state, action) => {
const { message } = action.payload;
console.log("successed", message);
state.response = action.payload;
state.status = "succeeded";
});
builder.addCase(postMakerList.rejected, (state, action) => {
console.log("failed");
state.status = "failed";
});
},
});
export const responseSelect = (state: RootState) => state.organazation.response;
export const messageSelect = createSelector(
responseSelect,
(res) => res.message
);
export default OrganazationSlice.reducer;
就像标题一样,action.playload 具有值,但是当我尝试访问 playload 中的嵌套值时,我得到了未定义的值。我想获得部分响应(如消息或列表)并显示 在 html 上我该怎么做。
我正在使用 useEffect 来发送 postMakerList
const dispatch = useAppDispatch();
const response = useAppSelector(responseSelect);
const message = useAppSelector(messageSelect);
useEffect(() => {
dispatch(postMakerList());
}, []);
事实上,当我使用 response 时,它会显示在 html 中,但是当我使用 message 或 response.message 时 不要出现。
【问题讨论】:
-
尝试将代码添加到沙箱并共享链接
标签: reactjs next.js react-redux redux-toolkit