- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
到目前为止,与其他 Flux 实现相比,我更喜欢 Redux,并且我正在使用它来重写我们的前端应用程序。
我面临的主要困难点:
第一个问题可以通过在每种数据类型的子状态中保留一个状态字段来解决。例如:
function postsReducer(state, action) {
switch(action.type) {
case "FETCH_POSTS":
return {
...state,
status: "loading",
};
case "LOADED_POSTS":
return {
status: "complete",
posts: action.posts,
};
}
}
function commentsReducer(state, action) {
const { type, postId } = action;
switch(type) {
case "FETCH_COMMENTS_OF_POST":
return {
...state,
status: { ...state.status, [postId]: "loading" },
};
case "LOADED_COMMENTS_OF_POST":
return {
status: { ...state.status, [postId]: "complete" },
posts: { ...state.posts, [postId]: action.posts },
};
}
}
现在我可以为帖子制作一个传奇,为评论制作另一个传奇。每个 Sagas 都知道如何获取请求的状态。但这会很快导致大量重复代码(例如帖子、评论、点赞、 react 、作者等)。
我想知道是否有避免所有重复代码的好方法。
当我需要通过 ID 从 redux 商店获取评论时,第二个问题就出现了。是否有处理数据之间关系的最佳做法?
谢谢!
最佳答案
redux-saga now has takeLeading(pattern, saga, ...args)
redux-saga 的 1.0+ 版本具有 takeLeading,它会在每个与模式匹配的 Store 中派发一个 Action 时产生一个传奇。生成一次任务后,它会阻塞,直到生成的传奇完成,然后再次开始监听模式。
之前我实现了 this solution来自 Redux Saga 的所有者,它工作得非常好——我从 API 调用中得到错误,有时会被触发两次:
You could create a higher order saga for this, which would look something like this:
function* takeOneAndBlock(pattern, worker, ...args) {
const task = yield fork(function* () {
while (true) {
const action = yield take(pattern)
yield call(worker, ...args, action)
}
})
return task
}
and use it like this:
function* fetchRequest() {
try {
yield put({type: 'FETCH_START'});
const res = yield call(api.fetch);
yield put({type: 'FETCH_SUCCESS'});
} catch (err) {
yield put({type: 'FETCH_FAILURE'});
}
}
yield takeOneAndBlock('FETCH_REQUEST', fetchRequest)
In my opinion this way is far way more elegant and also its behaviour can be easily customized depending on your needs.
关于javascript - 如何使用 Redux-Saga 避免重复的 API 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37040823/
有什么方法可以将来自 node_modules 的 sagas 与我为我的应用程序编写的其他 sagas 结合起来吗?如果 sagaMiddleware.run() 接受 sagas 数组,但 id
我熟悉redux-thunk,今天来redux-saga,我知道当我们要处理异步的时候,redux-saga需要先调用一个actions作为signal,然后sagas来处理,返回成功/失败操作,所以
如果我有两个 sagas 在同一 Action 上等待 yield take(),是否可以保证哪个 saga 将首先选择该 Action 并执行其剩余逻辑或者是随机的?我需要确保第一个传奇在第二个传奇
我一直在关注 AxonBank example为了理解 Axon 框架中 Saga 的实现,并有一些像这样的代码来开始和结束 saga @Saga public class MoneyTransfer
我有一个基本的传奇,如下所示: const mySaga = function* () { yield takeEvery("SOME_ACTION_REQUEST", function* (
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在开发一个使用 redux-saga 的项目管理来自 API 调用的状态更改。 我看过很多关于如何对传奇进行单元测试的例子,但很少有例子可以用来实际进行集成测试。我们正在使用 jest对于断言。
我有下一个代码: const sagaMiddleware = createSagaMiddleware(options); . . . const createdStore = createStor
当调度一个 Action 时,它到达 reducer 和传奇的顺序是否得到保证? 我可以信赖它 首先进入reducer 那么传奇呢? reducer : function reducer(state
我们的应用程序使用尝试 - 成功 - 失败的方法来处理来自服务器的响应。 我有一个生成器函数,需要像这样运行: function * getSingleSectorAttempt(action) {
当它在屏幕上可见时,我正在记录横幅展示次数。 当用户滚动时,同一条横幅可以在短时间内多次显示。 我想防止这种情况发生。 乍一看,throttle是防止它的完美方法。 但是,当您在一页中有多个横幅时,t
我在 containers/App/sagas.js 中有一个 sagas.js,它包含一个登录 saga,我试图从一个登录弹出窗口调用它,它也有自己的 sagas.js (components/Lo
我有一个 Saga,我需要在其中执行 3 个异步请求,然后在后续请求中使用这 3 个请求的响应。这里有一些伪代码来解释: function* useOtherActionsAndSagas(actio
所以,我是 react 样板的新手,似乎没有办法在不影响前一个传奇的功能的情况下包含另一个传奇(即它不起作用)。 我尝试将 sagas 声明为常量,然后将其传递到容器末尾的 compose 函数中,但
我有以下情况: export function* addCircle(circleApi, { payload }) { try { const response = yiel
以前也有人问过类似的问题,但答案对我没有任何帮助。 What are selectors in redux? How to get something from the state / store i
我有一个运行良好的传奇,我调用一个操作并包含一些数据,传奇被触发,它从操作中提取值,执行API调用并以几个yield put结束 - 太棒了。 我现在正在尝试测试该传奇并解决问题,我已经编写了一个简单
我使用 redux-saga 的主要原因之一是它进行异步函数调用的可测试性。我的困境是,当我使用不属于我的 redux 存储的有状态对象进行编程时,使用 sagas 进行编程变得非常尴尬。是否有使用非
我是传奇世界的新手。虽然我在 react-native 领域使用过 thunk,但此刻我很困惑。我正在努力让我的项目的骨架继续运行,我希望它很快就会变得非常大。考虑到这一点,我试图将逻辑分成多个文件。
我正在使用 saga eventChannel 来监听正在触发的事件(可能是实际应用程序中的 WebSocket),然后我正在更新我的 Redux Store。在组件中,我正在调用 API 操作。然后
我是一名优秀的程序员,十分优秀!