• Stars
    star
    122
  • Rank 282,988 (Top 6 %)
  • Language
    JavaScript
  • Created almost 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React bindings for derivable state computation library

React Derivable

Travis build status npm

React Derivable allows to define React components which re-render when reactive values (defined in terms of derivable) used in render() change.

Table of Contents

Installation

Install from npm (react and derivable are peer dependencies and must be installed for an application too):

% npm install react
% npm install [email protected]
% npm install react-derivable

Usage

Define your application state in terms of derivable:

import {atom} from 'derivable'

let message = atom('Hello, World!')

Define a React component which accepts and uses in render a reactive value message:

import React from 'react'

let Hello = props =>
  <div>{props.message.get()}</div>

Now produce a new reactive component using higher-order reactive component

import reactive from 'react-derivable'

let ReactiveHello = reactive(Hello)

Render <ReactiveHello /> into DOM and pass it a reactive message value:

import ReactDOM from 'react-dom'

ReactDOM.render(<ReactiveHello message={message} />, ...)

Each time reactive value updates - component gets rerendered:

message.set('Works!')

API

reactive(Component)

As shown in the usage section above reactive(Component) decorator produces a reactive component out of an original one.

Reactive components re-render when one of the reactive values referenced from within render() change.

import React from 'react'
import {reactive} from 'react-derivable'

let ReactiveFunctional = reactive(props =>
  <div>{props.message.get()}</div>)

let ReactiveClassBased = reactive(class extends React.Component {

  render() {
    return <div>{this.props.message.get()}</div>
  }
})

pure(Component)

Makes component reactive and defines shouldComponentUpdate which compares props and state with respect to reactive values.

That allows to get rid of unnecessary re-renders.

import React from 'react'
import {pure} from 'react-derivable'

let PureFunctional = pure(props =>
  <div>{props.message.get()}</div>)

let PureClassBased = pure(class extends React.Component {

  render() {
    return <div>{this.props.message.get()}</div>
  }
})

pure(Component).withEquality(eq)

Same as using pure(Component) but with a custom equality function which is used to compare props/state and reactive values.

Useful when using with libraries like Immutable.js which provide its equality definition:

import * as Immutable from 'immutable'
import {pure} from 'react-derivable'

let Pure = pure(Component).withEquality(Immutable.is)

Guides

Local component state

React has its own facilities for managing local component state. In my mind it is much more convenient to have the same mechanism serve both local component state and global app state management needs. That way composing code which uses different state values and updates becomes much easier. Also refactorings which change from where state is originated from are frictionless with this approach.

As any component produced with reactive(Component) reacts on changes to reactive values dereferenced in its render() method we can take advantage of this.

Just store some atom on a component instance and use it to render UI and update its value when needed.

That's all it takes to introduce local component state:

import {Component} from 'react'
import {atom} from 'derivable'
import {reactive} from 'react-derivable'

class Counter extends Component {

  counter = atom(1)

  onClick = () =>
    this.counter.swap(value => value + 1)

  render() {
    return (
      <div>
        <div>{this.counter.get()}</div>
        <button onClick={this.onClick}>Next</button>
      </div>
    )
  }
}

Counter = reactive(Counter)

Flux/Redux-like unidirectional data flow

Flux (or more Redux) like architecture can be implemented easily with reactive values.

You would need to create a Flux architecture blueprint as a function which initialises an atom with some initial state and sets up action dispatching as a reducer (a-la Redux):

import {atom} from 'derivable'

function createApp(transformWithAction, initialState = {}) {
  let state = atom(initialState)
  return {
    state: state.derive(state => state),
    dispatch(action) {
      let transform = transformWithAction[action.type]
      state.swap(state => transform(state, action))
    }
  }
}

Now we can use createApp() function to define an application in terms of initial state and actions which transform application state:

const CREATE_TODO = 'create-todo'

let todoApp = createApp(
  {
    [CREATE_TODO](state, action) {
      let todoList = state.todoList.concat({text: action.text})
      return {...state, todoList}
    }
  },
  {todoList: []}
)

function createTodo(text) {
  todoApp.dispatch({type: CREATE_TODO, text})
}

Now it is easy to render app state into UI and subscribe to state changes through the reactive(Component) decorator:

import React from 'react'
import {reactive} from 'react-derivable'

let App = reactive(() =>
  <ul>
    {todoApp.state.get().todoList.map(item => <li>{item.text}</li>)}
  </ul>
)

Binding to external state sources

Sometimes state is originated not from application but from some external sources. One notorious example is routing where state is stored and partially controlled by a browser.

It is still useful to have access to that state and do it using the homogenous API.

Like we already discovered we can use derivable library to implement local component state and flux like state management easily. Let's see how we can use derivable to implement routing based on browser navigation state (HTML5 pushState API).

We'll be using the history npm package which makes working with HTML5 API smooth and simple.

First step is to make a history object which will hold the navigation state and some methods to influence those state:

import {createHistory as createBaseHistory} from 'history'
import {atom} from 'derivable'

function createHistory(options) {
  let history = createBaseHistory(options)
  let location = atom(history.getCurrentLocation())
  history.listen(loc => location.set(loc));
  history.location = location.derive(location => location)
  return history
}

let history = createHistory()

Now to build the router we just need to use history.location value in render():

let Router = reactive(props => {
  let {pathname} = history.location.get()
  // Any complex pathname matching logic here, really.
  if (pathname === '/') {
    return <Home />
  } else if (pathname === '/about') {
    return <About />
  } else {
    return <NotFound />
  }
})

Now to change location you would need another component which transforms location state: Link. Also it could track "active" state (if link's location is the current location):

let Link = reactive(props => {
  let {pathname} = history.location.get()
  let className = pathname == props.href ? 'active' : ''
  let onClick = e => {
    e.preventDefault()
    history.push(props.href)
  }
  return <a {...props} onClick={onClick} className={className} />
})

Lifting regular React components to work with derivable values

If you already have a React component which works with regular JS values but want it to work with derivable values you can use this little trick:

import {atom, unpack} from 'derivable'
import {reactive} from 'react-derivable'

class Hello extends React.Component {

  render() {
    return <div>{this.props.message}</div>
  }
}

let ReactiveHello = reactive(props =>
  <Hello {...props} message={props.message.get()} />)

<ReactiveHello message={atom('Hi')} />

Also because you are passing values as plain props they are going to participate in React component lifecycle as usual (e.g. you can access prev values in componentDidUpdate):

class Hello extends React.Component {

  render() {
    return <div>{this.props.message}</div>
  }

  componentDidUpdate(prevProps) {
    if (prevProps.message !== this.props.message) {
      // do something!
    }
  }
}

let ReactiveHello = reactive(props =>
  <Hello {...unpack(props)} />)

Examples

See examples in examples directory.

More Repositories

1

autobind-decorator

Decorator to automatically bind methods to class instances
JavaScript
1,448
star
2

reactify

[DEPRECATED] Browserify transform for JSX (superset of JavaScript used in React library by Facebook)
JavaScript
689
star
3

react-css-components

Define React presentational components with CSS
JavaScript
676
star
4

react-fa

DEPRECATED: use https://github.com/FortAwesome/react-fontawesome instead
JavaScript
492
star
5

react-async

[DEPRECATED] Asynchronously fetch data for React components
JavaScript
446
star
6

react-quickstart

[DEPRECATED] React project template with server-side UI rendering and routing
JavaScript
370
star
7

react-time

Component for React to render relative and/or formatted dates into <time> HTML5 element
JavaScript
211
star
8

styling

Create CSS modules with the full power of JavaScript
JavaScript
130
star
9

sitegen

Generate websites by composing React components
JavaScript
118
star
10

reactdown

Markdown based live document format
JavaScript
116
star
11

rrouter

Declarative routing layer for React applications
JavaScript
114
star
12

backbone.projections

backbone.projections is a set of projections for Backbone.Collection
CoffeeScript
110
star
13

validated

Validate your configurations with precise error messages
JavaScript
91
star
14

typescript-loader

[DEPRECATED] TypeScript Webpack Plugin
JavaScript
82
star
15

react-flexgrid

Flexbox Grid reimagined as a set of React components
JavaScript
73
star
16

less2stylus

[NOT MAINTAINED] LESS to Stylus source to source convertor capable of translating Bootstrap
CoffeeScript
71
star
17

xcss

xCSS is a library for programmatic stylesheet composition
JavaScript
63
star
18

backbone.viewdsl

Declarative view technology for Backbone
JavaScript
40
star
19

react-stylesheet

[DEPRECATED] A component for React to declare stylesheet dependencies for your reusable components
JavaScript
40
star
20

cssobjectify

Browserify transform to turn stylesheets into JSON objects
JavaScript
39
star
21

todomvc-flux-swarm

JavaScript
38
star
22

es6-template-strings-jsx

JavaScript
38
star
23

rethemeable

Utilities for producing and consuming themable React components
JavaScript
35
star
24

react-app-express

*DEPRECATED* React + Express + Browserify + History API + Server Side Rendering
JavaScript
32
star
25

react-app-controller

*DEPRECATED* React application controller to manage top-level React components according to window.location
JavaScript
32
star
26

type-systems

Playing with type systems
OCaml
32
star
27

routr

Request routing for WebOb based WSGI applications
Python
29
star
28

memoize-decorator

Memoize getters and methods to compute only once
JavaScript
29
star
29

sweet-assertions

Syntax for writing informative testing assertions
JavaScript
29
star
30

sweetify

Browserify transform for using Sweet.js macros
JavaScript
29
star
31

upaas

ΞΌPaaS β€” nano PaaS based on Docker and gitreceive
Shell
28
star
32

rescript

[PoC] Rescript is a scripting runtime for ReasonML
OCaml
25
star
33

webpack-package-loaders-plugin

Webpack module loaders discovery through package.json
JavaScript
25
star
34

julia-repl-vim

Julia REPL plugin for vim/neovim
Julia
24
star
35

react-app

*DEPRECATED* Rapid appliaction development with React
JavaScript
23
star
36

ctags-webpack-plugin

Webpack plugin to generate accurate ctags
JavaScript
21
star
37

purescript-node-thunk

Node callbacks as thunks
PureScript
20
star
38

console-ui

Composable console output (somewhat inspired by React)
JavaScript
20
star
39

rrun

[WIP] rrun allows to seamlessly run Reason/OCaml code with native speed
OCaml
19
star
40

sweet-jsx

Use JSX and sweet.js macros together
JavaScript
18
star
41

ppx_let_promise

Like async/await syntax for Promises in JS but for OCaml
OCaml
18
star
42

K.jl

K programming language dialect embedded in Julia
Julia
17
star
43

jsonpublish

Configurable JSON encoder for publishing Python objects as JSON documents
Python
17
star
44

markstruct

Block-based structured editor for Markdown
JavaScript
16
star
45

es6-module-jstransform

ES6 module syntax to CommonJS transformation
JavaScript
16
star
46

BQN.jl

BQN implementation in Julia
Julia
16
star
47

vim-flow-outline

Outline for JS modules with Flow
Vim Script
16
star
48

rework-macro

Macro CSS transform for rework/xcss
JavaScript
16
star
49

esy-docker

A set of make rules to produce docker images for esy projects
Makefile
15
star
50

configure

Configuration toolkit based on YAML
Python
15
star
51

mocha-doctest

Test your documentation
JavaScript
14
star
52

connect-browserify

Connect/express middelware for serving front-end applications with browserify.
JavaScript
13
star
53

YouTubeManager

YouTubeManager is an wrapper for YouTube JS Player API which tries to mimic SoundManager2 API.
CoffeeScript
13
star
54

domain-context

Globally accessible domain-bound contexts, connect/express middleware included
CoffeeScript
12
star
55

babel-plugin-ast-literal

Babel Plugin AST Literal
JavaScript
11
star
56

react-custom-events

Don't use this, this was an experiment and it doesn't work anymore with recent versions of React
JavaScript
11
star
57

react-image-size-loader

Webpack loader for images which turns them into <img /> components with height and width
JavaScript
11
star
58

wpack

JavaScript
10
star
59

jstransformify

Browserify transform which applies jstransform visitors
JavaScript
9
star
60

react-macros

A set of syntax extensions for React
JavaScript
8
star
61

backbone.viewevents

Events for Backbone.View which can bubble up through view hierarchy.
JavaScript
7
star
62

prefetch-context-webpack-plugin

Webpack plugin which prefetches context (all files within the directory tested by a regular expression)
JavaScript
7
star
63

musvox

Collaborative music listening environment for Minecraft-like voxel worlds
JavaScript
7
star
64

lang-julia

Julia language support for the CodeMirror code editor
TypeScript
7
star
65

react-dom-events

**DO NOT USE THIS**
JavaScript
7
star
66

sphinx-npm

Sphinx documentation tool launcher which builds docs for npm packages
JavaScript
6
star
67

dream-totp-auth

An example Dream app with password auth & totp
OCaml
6
star
68

extracty

a set of tools to extract metadata from HTML documents (WIP)
Python
6
star
69

bw_sphinxtheme

Sphinx theme in black and white colours.
JavaScript
6
star
70

inets_mod_proxy

Simple HTTP proxy module for erlang inets httpd service.
Erlang
6
star
71

swarm-react

JavaScript
5
star
72

esy-bsb-example

OCaml
5
star
73

stream-rpc

RPC over arbitrary streams for Node.js and a browser
CoffeeScript
5
star
74

require-assets

A library to package and re-use static assets
JavaScript
5
star
75

asyncomplete-ale.vim

LSP completion source (via ALE) for asyncomplete.vim
Vim Script
5
star
76

react-router-component-bower

Bower package for react-router-component
JavaScript
5
star
77

deps-topo-sort

Sort module-deps/dgraph output topologically
JavaScript
5
star
78

fzf-merlin

Vim Script
5
star
79

diffbot

DiffBot API wrapper (uses urllib3)
Python
5
star
80

purescript-immutable

PureScript bindings to Immutable.js library
PureScript
5
star
81

contentlet

Framework for creating composable and reusable web UI.
Python
5
star
82

aoc2021

AOC2021 in BQN
5
star
83

webpack-stylegen

JavaScript
5
star
84

backbone.module

Spine.Module but for Backbone
CoffeeScript
4
star
85

dgraph

Build and transform dependency graphs from JS, CSS or other code bases
JavaScript
4
star
86

sphinxalchemy

SphinxQL dialect for SQLAlchemy (uses MySQLdb-Python for wire protocol)
Python
4
star
87

es6-destructuring-jstransform

ES6 destructuring assignment and destructuring function arguments transformation.
JavaScript
4
star
88

reason-react-workshop

CSS
4
star
89

wall

Extremely hackable HN/Reddit clone in PostgreSQL + Node + Express + React
CoffeeScript
4
star
90

esy-solve-cudf

Makefile
4
star
91

esy-reason-graphql-server

[EXAMPLE REPO, NOT MAINTAINED] Reason + GraphQL on esy
OCaml
4
star
92

faviconr

Fast and robust favicon resolution
CoffeeScript
4
star
93

jsxx

JSX eXperimental
JavaScript
4
star
94

ipsql

Intelligent PostgreSQL shell (concept)
Python
3
star
95

pureact

PureScript
3
star
96

ppx_router

type safe routing for Dream
OCaml
3
star
97

docgen.mk

a set of utilities and make macros for static site generation
Python
3
star
98

fb.py

Python bindings for Facebook Graph API
Python
3
star
99

react-async-middleware

Connect/express middleware to serve react-async components
JavaScript
3
star
100

react-pad

Authoring tool for React components
JavaScript
3
star