gpt4 book ai didi

javascript - Epic 不在 Redux-Observable 中返回流

转载 作者:行者123 更新时间:2023-12-04 00:34:24 28 4
gpt4 key购买 nike

我正在测试使用 redux-observable 和一个副项目,我反复遇到这个问题:Uncaught TypeError: combineEpics: one of the provided Epics "handleSearchEpic"不返回流。仔细检查您没有遗漏返回语句!

我已经引用了 redux observable 文档和其他几个在线示例,但我无法确定我可能遗漏了什么。以下是我的行为和相关的史诗。

export const searchContent = query => {
return {
type: SEARCH_CONTENT,
query
}
}

const returnSearchContent = searchResults => {
return function(dispatch) {
dispatch({
type: RETURN_SEARCH_CONTENT,
searchResults
});
}
}

// Epics
export const handleSearchEpic = action$ => {
action$.ofType(SEARCH_CONTENT)
.mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
.map(res => returnSearchContent(res))
}

export const rootEpic = combineEpics(
handleSearchEpic
);

这是应用程序的根目录和商店配置:

const epicMiddleware = createEpicMiddleware(rootEpic);
const store = createStore(Reducer, applyMiddleware(epicMiddleware));

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);

最佳答案

您的handleSearchEpic 史诗是一个带有 block 的箭头函数,但实际上并不返回流。

不好

export const handleSearchEpic = action$ => { // <-- start of block
// vvvvvvv missing return
action$.ofType(SEARCH_CONTENT)
.mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
.map(res => returnSearchContent(res))
} // <-- end of block

很好

export const handleSearchEpic = action$ => {
return action$.ofType(SEARCH_CONTENT)
.mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
.map(res => returnSearchContent(res))
}

隐式返回?

或者,您可以删除 block 并使用隐式返回,这可能正是您想要做的?

export const handleSearchEpic = action$ => // <--- no block
action$.ofType(SEARCH_CONTENT)
.mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
.map(res => returnSearchContent(res));

一个非常常见的错误,这就是为什么我添加了您提供的错误消息,但它似乎并没有使解决方案易于理解。有什么建议可以改进错误消息吗?

combineEpics: one of the provided Epics "handleSearchEpic" does not return a stream. Double check you're not missing a return statement!

关于javascript - Epic 不在 Redux-Observable 中返回流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43959015/

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