8000 GitHub - garbles/react-observable-subscribe2: :poultry_leg: A React component that consumes observables as props and maps them to a function
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

garbles/react-observable-subscribe2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Observable Subscribe 2

Inspired by Jay Phelps' React Observable Subscribe, this library exposes a single component - <Subscribe> - which consumes observables as props and maps them to a function.

Wat?

So I stole a bunch of code that I wrote from Yolk.js to make this happen. Here's an example.

import {render} from 'react-dom'
import {Subscribe} from 'react-observable-subscribe2'
import {Observable} from 'rxjs/Observable'
import {BehaviorSubject} from 'rxjs/BehaviorSubject'

const message$ = Observable.of(`Welcome`)
const name$ = new BehaviorSubject(`Gabe`)

render (
  <Subscribe message={message$} name={name$}>
    {props =>
      <span>{props.message}, {props.name}</span>
    }
  </Subscribe>,
  document.querySelector('#app')
)

// renders <span>Welcome, Gabe</span>

Pushing a new value to name$ will update the view accordingly. Additionally, replacing the child function will immediately invoke the new one and replace the view. The component will render a <noscript /> until all observables have passed at least one value to the subscriber.

Subscribe also allows you just wrap your function components like so.

function MyComponent ({message, name}) {
  return <span>{props.message}, {props.name}</span>
}

<Subscribe message={message$} name={name$}>{MyComponent}</Subscribe>

Inspiration

React Observable Subscribe

Cycle JS

Yolk JS

About

🍗 A React component that consumes observables as props and maps them to a function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0