问题标签 [redux-observable]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
2775 浏览

redux-observable - RxJS 有没有一种方法可以在不构建 url 的情况下使用参数进行 ajax 获取请求

我无法将参数传递给 ajax 获取请求。假设我必须将参数传递{category: 'cat', type: 'type', searchKey: 'key' }给 url /search,并且我有以下代码:

由于我是 RxJs 的新手,请建议我这样做的正确方法。

0 投票
2 回答
2704 浏览

redux-observable - Redux-observable 史诗调度两个动作

我在 redux-observable 中有以下史诗

到目前为止我很好,但是我希望史诗发送一个 'LOADING_USERS' 动作,以便我可以在加载用户时显示处理消息。史诗是这样做的正确地方,如果是这样,我该怎么做。如果史诗不是这样做的地方,那么我在哪里做呢?

0 投票
1 回答
829 浏览

redux - 测试 redux-observable 史诗的错误处理

在这里,我正在使用redux-observable并且在测试错误处理时遇到了一些问题。

请参阅以下注销用户史诗:

这是一个模拟graphQl服务的测试,以确保它在承诺拒绝时触发失败操作:

在测试中,它调度的是在 mergeMap 中展平的动作,而不是在 catch 中预期的失败动作。

像这样设置错误处理是否有效,或者我是否表现出我对 RxJs 的无知?

0 投票
3 回答
5458 浏览

oop - Is there any place for OOP in redux?

I've been using object-oriented programming practices for 25 years and trying to move toward functional programming for the last 5 years, but my mind always goes towards OOP when I'm trying to do something complex and, especially now that ES6 supports decent OOP syntax, that's the natural way for me to build stuff.

I'm now learning Redux and I understand (c.f. How to put methods onto the objects in Redux state?) that it's a no-no to put class instances in your reducers; and the recommended method for computing on top of plain reducer state is by using selectors (e.g., via reselect). And, of course, React recommends composition over inheritance (https://facebook.github.io/react/docs/composition-vs-inheritance.html, React redux oop classes).

But is there any place in the React/Redux ecosystem for class objects with methods and inheritance?

I guess, to sort of answer my own question, OOP classes encourage the addition of data properties and operations on the data in the same place, which is nice for readability, but doesn't fit well with pure functions and immutable data.

If I was going to use OOP, would I need to chuck the idea of having my instances persist and maintain state for any amount of time? Like, every time I want to use one, I would instantiate it from store data, use whatever methods I want, and throw it away? That might obviate a lot of the impetus to use OOP classes. But if I keep instances around, I'll have headaches keeping them synced with the store.

So, is the answer to always use selectors when I'm tempted to use methods and always use composition when I'm tempted to use inheritance? Specifically, I mean when storing and manipulating data held in a Redux store for use in React components. And, if so, where should it fit in? Connected to selectors? Immediately disposable like I suggested?


Adding my use case for clarity: My data is basically a huge graph: lots of objects with lots of properties and lots of relationships between objects. It's read only, but complex. My objects are called "concepts".

Before making the (probably foolish) decision to migrate to Redux, I used classes to structure and represent concepts, sets of concepts, and relationships between concepts. My classes included async api logic to fetch concept sets, information about each concept, and information about other concepts that each concept is related to. If the user chose to drill down, the classes would recursively fetch and instantiate new concept sets. The Redux documentation recommends flat, normalized structures for nested data (http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html) which is probably wise for storage, but my OOP model was good for traversing sections of the graph and stuff. I have a hard time wrapping my head around using selectors and immutable state that might involve nesting, potentially with cycles, or needing to make async calls for more data.

I'm successfully using https://redux-observable.js.org/ for the api stuff.

Maybe @Sulthan's answer is right: I should feel free to use OOP techniques in my Redux application. But it still seems weird. I can't keep my objects around because if the store changes (more data is fetched, for instance), my objects can get stale. If my objects are nested but my store is normalized, I'll instantiate them (from selectors) when I need them and make sure not to keep them around...

0 投票
1 回答
924 浏览

react-native - redux-observable 获取当前位置

我正在尝试使用反应原生地理定位getCurrentPosition然后一旦返回位置,使用反应原生地理编码器使用该位置来获取位置。我正在使用redux-observable史诗来完成所有这些工作。

这是我的两部史诗:

location.epic.js

应用程序启动后,此代码将立即执行:

fetchCategories()也是一个有史诗的动作,但这是有效的。调度getCurrentPosition()动作贯穿上面的史诗。我能看到的唯一输出是我的减速器getLocationRejected()在控制台记录以下内容时处理:

这是我的减速器:

有什么明显的我做错了吗?我通过添加进行调试的尝试.do(x => console.log(x))什么也没做,控制台没有记录任何内容。updateRegion()永远不会触发,因为它会分派一个动作,而减速器UPDATE_REGION永远不会执行。但是执行必须使其成为成功案例,getCurrentPosition()例如:

必须执行,因为getCurrentLocation(position)确实被调度了。

我哪里错了?

0 投票
1 回答
364 浏览

rxjs - RXJS + Redux Observable - 如何合并一个新流?

我们最近选择了 Redux Observable,它是管理高级操作编排的好方法。

我最近遇到的一个问题是响应数据获取的结果。我们有一个通用的服务函数,它返回一个 RXJS observable。通常我们选择我们需要的任何数据并订阅。

我认为这对于 redux-observable 来说是相当自然的。在 Epic 上使用 MapTo,并在随后的选择中返回 RXJS 观察者。

据我所知,Redux-observable 没有订阅,所以什么也没有发生。

有没有人有它应该如何工作的例子?

export function redirectUserToEndpointEpic(action$) { return action$.ofType(LOCATION_CHANGE) .filter(action=>action.payload.pathname !== '/')) .mapTo(action=>authService.getObserver() // returns a stream which has not been subscribed to .select(userData=>userData.defaultPath) .map(push); }

0 投票
1 回答
1617 浏览

react-native - redux-observable Actions 必须是普通对象。使用自定义中间件进行异步操作

getCurrentPositionEpic当我在下面的代码中注释掉时,该应用程序可以工作。但是,如果我不加注释,我会收到错误消息:

动作必须是普通对象。使用自定义中间件进行异步操作。

location.epic.js

下面的代码只是一个帮助器,用于将本机反应转换navigator.geolocation.getCurrentPosition为可观察的,而不是接受回调的函数。

callBackToObservable.js

什么可能导致错误?

尝试通过商店:

使用https://github.com/devfd/react-native-geocoder _Geocoder.geocodePosition

0 投票
1 回答
1168 浏览

redux-observable - redux-observable 返回多种动作类型

我正在使用 redux-observable,这就是我想要做的。

  1. 当 'APPLY_SHOPPING_LIST' 的动作类型进入调度 'APPLYING_SHOPPING_LIST' 并在 5 秒后调度'APPLIED_SHOPPING_LIST'。这是我到目前为止提出的代码

    返回连接列表;

现在的问题是只有 'APPLYING_SHOPPING_LISTS' 被触发,'APPLIED_SHOPPING_LISTS' 根本不会被触发到减速器。我在这里错过了什么吗?

只是为了补充一点,当我使用 flatMap 它工作时,下面给出的是代码

我很困惑这是如何工作的,而另一个没有?

0 投票
1 回答
451 浏览

react-router - 一旦动作触发,redux-observable 执行转换

我将 redux-observable 与史诗一起使用。

一旦史诗完成触发“APPLIED_SHOPPING_LISTS”我想执行转换,我正在使用react-router。这样做的最佳地点是什么?我看到了 redux-history-transitions,这是我应该使用的插件吗?

除此之外,我确实使用了 redux-history-transitions 并将其更改为以下

这似乎确实改变了 url 并发生了转换,但是我在“/Shopping”路径下配置的组件不会呈现。它只是停留在当前页面上。这就是我的路线的样子

0 投票
2 回答
1974 浏览

rxjs - Redux Observable:如何从回调中返回一个动作?

我正在使用具有非常特定 API 的 WebRTC 库。该peerConnection.setRemoteDescription方法的第二个参数应该是完成设置远程描述时的回调:

这是我的 WebRTC 类的包装函数之一:

这是我想做的草图:

这不起作用,因为我没有返回 Observable。有没有办法做到这一点?

PS这是WebRTC API:https ://github.com/oney/react-native-webrtc/blob/master/RTCPeerConnection.js#L176