Giter VIP home page Giter VIP logo

rxjs-react's Introduction

Hi there, I'm murakami. 👋

I'm a front-end developer and a haskell language lover.

  • 🌱 I’m currently learning parse and wasm
  • 🤔 I’m looking for help with principles of compilation
  • 💬 Ask me about javascript、haskell
  • 📫 How to reach me: Kyoto

Here is my github stats:

github stats

and my most used languages here:

most used languages

rxjs-react's People

Contributors

murakamikennzo avatar

Watchers

 avatar

rxjs-react's Issues

do this is a better way?

Here is another way when using rxjs in react:

import { Observable, merge, Subject, of, combineLatest } from 'rxjs'
import { map, mapTo, skip, startWith, scan } from 'rxjs/operators'
import React from 'react'
import ReactDOM from 'react-dom'

const createEventObservable = <T extends {} = any>(): [Observable<T>, (e: T) => void] => {
  let subject$ = new Subject<T>()

  const observable = subject$.asObservable()
  const handler = e => subject$.next(e)

  return [observable, handler]
}

const toState = <T extends {} = any>(reducer$: Observable<(state: T) => T>): Observable<T> => 
  reducer$.pipe(
    startWith(undefined as unknown),
    // @ts-ignore
    scan((x, f) => f && f(x)),
    skip(1),
  )

const App = (): Observable<JSX.Element> => {

  const [addVdom$, addReducer$] = Add()
  const [subtractVdom$, subtractReducer$] = Subtract()

  const reducer$ = of(() => 0)

  const state$ = toState( merge(addReducer$, subtractReducer$, reducer$) )

  return combineLatest(state$, addVdom$, subtractVdom$).pipe(
    map(([count, addVdom, subtractVdom]) => (
      <>
        <p>Current count: { count }</p>
        <p>{ addVdom } { subtractVdom }</p>
      </>
    ))
  )
}

const Add = (): [Observable<JSX.Element>, Observable<(x: number) => number>] => {
  const [observable, handler] = createEventObservable<React.MouseEvent<HTMLButtonElement, MouseEvent>>()

  return [
    of(<button onClick={handler}>add count</button>),
    observable.pipe(mapTo((x: number) => x + 1)),
  ]
}

const Subtract = (): [Observable<JSX.Element>, Observable<(x: number) => number>]  => {
  const [observable, handler] = createEventObservable<React.MouseEvent<HTMLButtonElement, MouseEvent>>()

  return [
    of(<button onClick={handler}>subtract count</button>),
    observable.pipe(mapTo((x: number) => x - 1)),
  ]
}

const vdom$ = App()

vdom$.subscribe(
  vdom => {
    ReactDOM.render(vdom, document.getElementById('app'))
  }
)

Do this is a better way?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.