Redux:无法将嵌套变量传递给操作
Redux : Cannot pass nested variable to action
我需要在 onClick
上将 suggestion._id
作为 response_id
传递给我的操作,但由于某些原因我在控制台中收到 [object%20Object]
。我尝试 response_id: suggestion._id
以先声明它,但没有成功。
操作:
export const deleteResponse = (id, response_id) => async dispatch => {
try {
await axios.delete(`/api/suggestions/response/${id}/${response_id}`);
dispatch({ type: DELETE_REPONSE, payload: response_id });
dispatch(setAlert('Review removed', 'success'));
} catch (err) {
dispatch({
type: RESPONSE_ERROR,
payload: {
msg: err.response,
status: err.response.status,
},
});
}
};
前端组件:
<Box mt='auto'>
<Flex justifyContent={'space-between'} mb='2'>
{suggestion._id}
<Box my='auto'>
{!auth.loading && user === auth.user._id && (
<Button
type='button'
onClick={(e) =>
deleteResponse(id, {
response_id: suggestion._id,
})
}
>
Remove
</Button>
)}
</Box>
</Flex>
</Box>;
这是因为你忘记解构了,需要这样解构:
export const deleteResponse = (id, { response_id }) => async dispatch => {
我需要在 onClick
上将 suggestion._id
作为 response_id
传递给我的操作,但由于某些原因我在控制台中收到 [object%20Object]
。我尝试 response_id: suggestion._id
以先声明它,但没有成功。
操作:
export const deleteResponse = (id, response_id) => async dispatch => {
try {
await axios.delete(`/api/suggestions/response/${id}/${response_id}`);
dispatch({ type: DELETE_REPONSE, payload: response_id });
dispatch(setAlert('Review removed', 'success'));
} catch (err) {
dispatch({
type: RESPONSE_ERROR,
payload: {
msg: err.response,
status: err.response.status,
},
});
}
};
前端组件:
<Box mt='auto'>
<Flex justifyContent={'space-between'} mb='2'>
{suggestion._id}
<Box my='auto'>
{!auth.loading && user === auth.user._id && (
<Button
type='button'
onClick={(e) =>
deleteResponse(id, {
response_id: suggestion._id,
})
}
>
Remove
</Button>
)}
</Box>
</Flex>
</Box>;
这是因为你忘记解构了,需要这样解构:
export const deleteResponse = (id, { response_id }) => async dispatch => {