【发布时间】:2020-02-16 01:09:05
【问题描述】:
代码如下
import { Action } from "redux";
import { ActionsObservable, ofType, combineEpics } from 'redux-observable'
import { map } from 'rxjs/operators'
export enum ActionTypes {
One = 'ACTION_ONE',
Two = 'ACTION_TWO'
}
export interface One extends Action {
type: ActionTypes.One
//commnent out next line to remove error
myStr: string
}
export const doOne = (myStr: string): One => ({
type: ActionTypes.One,
//comment out next line to remove error
myStr
})
export interface Two extends Action {
type: ActionTypes.Two
myBool: boolean
}
export const doTwo = (myBool: boolean): Two => ({ type: ActionTypes.Two, myBool })
export type Actions = One | Two
export const epic = (action$: ActionsObservable<Actions>) =>
action$.pipe(
ofType<Actions, One>(ActionTypes.One),
map((action: any) => ({
type: ActionTypes.Two,
myBool: true
}))
)
export const rootEpic = combineEpics(epic)
还有我如何生成商店
import { createEpicMiddleware } from "redux-observable"
import { createStore, applyMiddleware } from "redux"
import { reducer } from "./reducer"
import { rootEpic } from "./epic"
export const configStore = () => {
const epicMiddleware = createEpicMiddleware()
const store = createStore(reducer, applyMiddleware(epicMiddleware))
epicMiddleware.run(rootEpic)
return store
}
过去 4 个小时我一直在尝试将有效负载添加到操作中,但使用每种可能的过滤器解决方案都失败了。我尝试在 redux observable github 部分和他们的聊天室寻求帮助。我还搜索了严格输入的简单示例。没有找到任何解决方案。
【问题讨论】:
-
我能够通过使用 (action$:any) 而不是 (action$: ActionsObservable
) 让它工作,但它不是完美的解决方案。 -
这实际上是一个非常好的问题,我也想要一个更好的答案。
-
我最终输入了 rootEpic 作为从不表示它接受泛型类型(基本上)
-
我完全放弃了 redux,现在用普通的钩子保持全局状态。事实上,我很快就会完全放弃对最热门的新框架 sveltekit 的反应。
-
我刚刚开始使用 react 和 redux 重建我公司的应用程序,因为它遵循与 Angular 相同的模式。我对 svelt 的了解还不够,无法自信地推荐它作为一个框架。
标签: reactjs typescript redux redux-observable