我相信你应该避免在中间件中这样做。您可以使用已翻译的消息发送您的操作。
const deleteUser = (id, messages) => {
type: DELETE_USER,
payload: {id, messages}
}
然后在您的 saga(或其他中间件)中,您可以使用此已翻译的消息。
function* deleteUserWatcher({
payload: { id, messages }
}) {
try {
yield request.delete(`/user/${id}`);
yield put(deleteUserSuccess(id));
yield put(pushNotificationToStack(message.success));
} catch (error) {
yield put(pushNotificationToStack(message.error));
}
}
然后在您的组件中,您可以调度操作
const dispatch = useDispatch();
const { formatMessage } = useIntl();
const handleDeleteUser = id => {
dispatch(deleteUser(id, {
success: formatMessage({
id: "User.delete.success",
defaultMessage: "User has been deleted"
}),
error: formatMessage({
id: "User.delete.error",
defaultMessage: "Ups. Something went wrong. Sorry :("
}),
}
));
}
我知道这并不适合所有情况,但是您可以使用这种方法涵盖大多数情况