All Projects → recyclejs → Recycle

recyclejs / Recycle

Convert functional/reactive object description using RxJS into React component

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Recycle

Evolui
A tiny reactive user interface library, built on top of RxJs.
Stars: ✭ 43 (-88.5%)
Mutual labels:  rxjs, reactive-programming, observable
observe-component
A library for accessing React component events as reactive observables
Stars: ✭ 36 (-90.37%)
Mutual labels:  rxjs, observable, reactive-programming
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (-63.37%)
Mutual labels:  rxjs, reactive-programming, observable
rxjs-proxify
Turns a Stream of Objects into an Object of Streams
Stars: ✭ 34 (-90.91%)
Mutual labels:  rxjs, observable, reactive-programming
observable-playground
Know your Observables before deploying to production
Stars: ✭ 96 (-74.33%)
Mutual labels:  rxjs, observable, reactive-programming
Od Virtualscroll
🚀 Observable-based virtual scroll implementation in Angular
Stars: ✭ 133 (-64.44%)
Mutual labels:  rxjs, reactive-programming, observable
ng-observe
Angular reactivity streamlined...
Stars: ✭ 65 (-82.62%)
Mutual labels:  rxjs, observable, reactive-programming
BittrexRx
BittrexRx is a rxjs node wrapper for the Bittrex Api
Stars: ✭ 16 (-95.72%)
Mutual labels:  rxjs, observable
angular2-instagram
🔥Instagram like photo filter playground built with Angular2 (Web | Desktop)
Stars: ✭ 91 (-75.67%)
Mutual labels:  rxjs, reactive-programming
react-with-observable
Use Observables with React declaratively!
Stars: ✭ 108 (-71.12%)
Mutual labels:  rxjs, observable
Learn Rxjs
Clear examples, explanations, and resources for RxJS
Stars: ✭ 3,475 (+829.14%)
Mutual labels:  rxjs, reactive-programming
rxact
Rxact is an observable application management for Javascript app
Stars: ✭ 21 (-94.39%)
Mutual labels:  rxjs, observable
polyrhythm
A 3Kb full-stack async effect management toolkit over RxJS. Uses a Pub-Sub paradigm to orchestrate Observables in Node, or the browser (ala Redux Saga). Exports: channel, listen, filter, trigger, after.
Stars: ✭ 23 (-93.85%)
Mutual labels:  rxjs, observable
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-84.49%)
Mutual labels:  observable, reactive-programming
monogram
Aspect-oriented layer on top of the MongoDB Node.js driver
Stars: ✭ 76 (-79.68%)
Mutual labels:  rxjs, observable
rjax
base on rxjs awesome ajax library
Stars: ✭ 63 (-83.16%)
Mutual labels:  rxjs, observable
rx-ease
Spring animation operator for rxjs 🦚✨
Stars: ✭ 16 (-95.72%)
Mutual labels:  rxjs, observable
axios-for-observable
A RxJS wrapper for axios, same api as axios absolutely
Stars: ✭ 13 (-96.52%)
Mutual labels:  rxjs, observable
Swiftrex
Swift + Redux + (Combine|RxSwift|ReactiveSwift) -> SwiftRex
Stars: ✭ 267 (-28.61%)
Mutual labels:  reactive-programming, observable
Formily
Alibaba Group Unified Form Solution -- Support React/ReactNative/Vue2/Vue3
Stars: ✭ 6,554 (+1652.41%)
Mutual labels:  rxjs, observable

Join the chat at https://gitter.im/recyclejs npm version npm downloads

DEPRECATED

Please note that this library hasn't been updated for more than two years. It's very rarely used and I consider it deprecated.

Recycle

Convert functional/reactive object description into React component.

You don't need another UI framework if you want to use RxJS.

Installation

npm install --save recycle

Example

Webpackbin example

const Timer = recycle({
  initialState: {
    secondsElapsed: 0,
    counter: 0
  },
 
  update (sources) {
    return [
      sources.select('button')
        .addListener('onClick')
        .reducer(state => {
          ...state,
          counter: state.counter + 1
        }),
      
      Rx.Observable.interval(1000)
        .reducer(state => {
          ...state,
          secondsElapsed: state.secondsElapsed + 1
        })
    ]
  },
 
  view (props, state) {
    return (
      <div>
        <div>Seconds Elapsed: {state.secondsElapsed}</div>
        <div>Times Clicked: {state.counter}</div>
        <button>Click Me</button>
      </div>
    )
  }
})

You can also listen on child component events and define custom event handlers. Just make sure you specify what should be returned:

import CustomButton from './CustomButton'

const Timer = recycle({
  initialState: {
    counter: 0
  },
 
  update (sources) {
    return [
      sources.select(CustomButton)
        .addListener('customOnClick')
        .reducer((state, returnedValue) => {
          counter: state.counter + returnedValue
        })
    ]
  },
 
  view (props, state) {
    return (
      <div>
        <div>Times Clicked: {state.counter}</div>
        <CustomButton customOnClick={e => e.something}>Click Me</CustomButton>
      </div>
    )
  }
})

Replacing Redux Connect

If you are using Redux, Recycle component can also be used as a container (an alternative to Redux connect).

The advantage of this approach is that you have full control over component rerendering (components will not be "forceUpdated" magically).

Also, you can listen to a specific part of the state and update your component only if that property is changed.

export default recycle({
  dispatch (sources) {
    return [
      sources.select('div')
        .addListener('onClick')
        .mapTo({ type: 'REDUX_ACTION_TYPE', text: 'hello from recycle' })
    ]
  },

  update (sources) {
    return [
      sources.store
        .reducer(function (state, store) {
          return store
        })

      /** 
      * Example of a subscription on a specific store property
      * with distinctUntilChanged() component will be updated only when that property is changed
      *
      * sources.store
      *   .map(s => s.specificProperty)
      *   .distinctUntilChanged()
      *   .reducer(function (state, specificProperty) {
      *     state.something = specificProperty
      *     return state
      *   })
      */
    ]
  },

  view (props, state) {
    return <div>Number of todos: {store.todos.length}</div>
  }
})

Effects

If you don't need to update a component local state or dispatch Redux action, but you still need to react to some kind of async operation, you can use effects.

Recycle will subscribe to this stream but it will not use it. It is intended for making side effects (like calling callback functions passed from a parent component)

const Timer = recycle({
 
  effects (sources) {
    return [
      sources.select('input')
        .addListener('onKeyPress')
        .withLatestFrom(sources.props)
        .map(([e, props]) => {
          props.callParentFunction(e.target.value)
        })
    ]
  },
 
  view (props) {
    return (
      <input placeholder={props.defaultValue}></input>
    )
  }
})

API

Component description object accepts following properties:

{
  propTypes: { name: PropTypes.string },
  displayName: 'ComponentName',
  initialState: {},
  dispatch: function(sources) { return Observable },
  update: function(sources) { return Observable },
  effects: function(sources) { return Observable },
  view: function(props, state) { return JSX }
}

In update, dispatch and effects functions, you can use the following sources:

/**
*   sources.select
*
*   select node by tag name or child component
*/
sources.select('tag')
  .addListener('event')

sources.select(ChildComponent)
  .addListener('event')

/**
*   sources.selectClass
*
*   select node by class name
*/
sources.selectClass('classname')
  .addListener('event')

/**
*   sources.selectId
*
*   select node by its id
*/
sources.selectId('node-id')
  .addListener('event')

/**
*   sources.store
*
*   If you are using redux (component is inside Provider)
*   sources.store will emit its state changes
*/
  sources.store
    .reducer(...)

/**
*   sources.state
*
*   Stream of current local component state
*/
  sources.select('input')
    .addListener('onKeyPress')
    .filter(e => e.key === 'Enter')
    .withLatestFrom(sources.state)
    .map(([e, state]) => state.someStateValue)
    .map(someStateValue => using(someStateValue))

/**
*   sources.props
*
*   Stream of current local component props
*/
  sources.select('input')
    .addListener('onKeyPress')
    .filter(e => e.key === 'Enter')
    .withLatestFrom(sources.props)
    .map(([e, props]) => props.somePropsValue)
    .map(somePropsValue => using(somePropsValue))

/**
*   sources.lifecycle
*
*   Stream of component lifecycle events
*/
  sources.lifecycle
    .filter(e => e === 'componentDidMount')
    .do(something)

FAQ

Why would I use it?

  • Greater separation of concerns between component presentation and component logic
  • You don't need classes so each part of a component can be defined and tested separately.
  • Component description is more consistent. There is no custom handleClick events or this.setState statements that you need to worry about.
  • The State is calculated the same way as for redux store: state = reducer(state, action).
  • Redux container looks like a normal component and it's more clear what it does.
  • Easy to use in an existing React application (choose components which you wish to convert).

Why would I NOT use it?

  • Observables are not your thing.
  • You need more control over component lifecycle (like shouldComponentUpdate)

What is this? jQuery?

No.

Although it resembles query selectors, Recycle uses React’s inline event handlers and doesn’t rely on the DOM. Since selection is isolated per component, no child nodes can ever be accessed.

Can I use CSS selectors?

No.

Since Recycle doesn't query over your nodes, selectors like div .class will not work.

How does it then find selected nodes?

It works by monkeypatching React.createElement. Before a component is rendered, for each element, if a select query is matched, recycle sets inline event listener.

Each time event handler dispatches an event, it calls selectedNode.rxSubject.next(e)

Can I use it with React Native?

Yes.

Recycle creates classical React component which can be safely used in React Native.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].