[Recompose] Merge RxJS Button Event Streams to Build a React Counter Component

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Recompose] Merge RxJS Button Event Streams to Build a React Counter Component相关的知识,希望对你有一定的参考价值。

Combining input streams then using scan to track the results is a common scenario when coding with streams. This lesson walks you through setting up two buttons and merging their input events together to build a streaming Counter component.

 

const CounterStream = componentFromStream(
  props$ => {

    const { stream: onInc$, handler: onInc } = createEventHandler();
    const { stream: onDec$, handler: onDec } = createEventHandler();

    return props$
      .switchMap(
      propos => Observable.merge(
        onInc$.mapTo(1),
        onDec$.mapTo(-1)
      )
        .startWith(propos.value)
        .scan((acc, curr) => acc + curr)
        .map((value) => ({ value, onInc, onDec })))
      .map(
      Counter
      )
  }
);

 

 

以上是关于[Recompose] Merge RxJS Button Event Streams to Build a React Counter Component的主要内容,如果未能解决你的问题,请参考以下文章

[Recompose] Stream Props to React Children with RxJS

[RxJS] Handling Multiple Streams with Merge

RxJS之组合操作符 ( Angular环境 )

[RxJS] Reactive Programming - New requests from refresh clicks -- merge()

RxJS Observable Share startWith() state in merge()

RxJS入门2之Rxjs的安装