zoukankan      html  css  js  c++  java
  • [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

    Rather than using Components to push streams into other Components, mapPropsStream allows you to create functions that can wrap components to create shareable behaviors across components.

    Using componentFromStream:

    import React from "react"
    import { render } from "react-dom"
    import { Observable } from "rxjs"
    import config from "recompose/rxjsObservableConfig"
    import {
      setObservableConfig,
      componentFromStream,
      createEventHandler,
      mapPropsStream
    } from "recompose"
    
    setObservableConfig(config)
    //#endregion
    
    const Counter = props => <h1>{props.count}</h1>
    
    const CounterWithInterval = componentFromStream(
      props$ => props$.switchMap(
        props => Observable.interval(1000),
        (props, count) => ({count, ...props})
      )
      .map(Counter)
    )
    
    
    const App = () => (
      <div>
        <CounterWithInterval />
      </div>
    )
    
    render(<App />, document.getElementById("root"))

    MapPropsStream allows you to create functions that will decorate your component, rather than creating a component itself. I'm going to create an interval here using MapPropsStream.

    A mapProposStream version can be like this:

    import React from "react"
    import { render } from "react-dom"
    import { Observable } from "rxjs"
    import config from "recompose/rxjsObservableConfig"
    import {
      setObservableConfig,
      componentFromStream,
      createEventHandler,
      mapPropsStream
    } from "recompose"
    
    setObservableConfig(config)
    //#endregion
    
    const Counter = props => <h1>{props.count}</h1>
    
    const interval = mapPropsStream(props$ => props$.switchMap(
      props => Observable.interval(1000),
      (props, count) => ({ count, ...props })
    ))
    
    const CounterWithInterval = interval(Counter)
    
    
    const App = () => (
      <div>
        <CounterWithInterval />
      </div>
    )
    
    render(<App />, document.getElementById("root"))

    As you can see, we take the hightlighted part as own function, wrapped with 'mapPropsStream'. 

  • 相关阅读:
    java日常问题和技巧1(BigDecimal与int相互转换、判断某元素是否在数组中、求两个List并集、int[]转Integer[])
    窗口小部件基础编写V1.0----没有Service
    使用MyBatis遇到的问题及解决方法(一)(持续更新)
    java工具类集合(一)
    idea部分操作(一)----持续更新
    单向链表(篇九)
    结构体(篇八)
    指针与字符串(篇七)
    数组字符串(篇六)
    循环与函数(篇五)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8087464.html
Copyright © 2011-2022 走看看