gpt4 book ai didi

javascript - 从从不同的缩减程序文件订阅的操作文件中分派(dispatch)操作是否是反模式?

转载 作者:行者123 更新时间:2023-12-02 23:25:18 31 4
gpt4 key购买 nike

通常在我的 Redux 项目中,我有成对出现的操作文件和 reducer 文件。因此,在我的 actions 文件夹中,我将有一个 posts.js,在我的 reducers 文件夹中,我还将有一个 posts.js。 posts 操作文件通常只调度在 posts 缩减程序文件中订阅的操作类型。

但现在我需要从 authors 缩减器文件订阅的 posts 操作文件中分派(dispatch)一个操作类型。这是可以的还是被认为是反模式?

最佳答案

这根本不是反模式。它具有良好的代码可重用性,对于错误处理特别有用。

考虑以下示例:

posts.js

import { GET_POSTS } from "./actions/types"
import { setErrors } from "./actions/errors"
import axios from "axios"

export const getPosts = () => {
return (dispatch) => {
axios.get("/api/posts")
.then((res) => {
dispatch({
type: GET_POSTS,
payload: res.data
})
})
.catch((errors) => {
dispatch(setErrors(errors.response.data))
}
}
}

错误.js

const setErrors = (errors) => {
return {
type: SET_ERRORS,
payload: errors
}
}

因此,与其在 posts.js 中定义一个全新的与帖子相关的 errors 操作,不如导入订阅您的错误的现有操作 - reducer (如果有)。对于同时使用作者发布操作也可以这样说。

关于javascript - 从从不同的缩减程序文件订阅的操作文件中分派(dispatch)操作是否是反模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56759751/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com