Simple React state management. Made with ❤️ and ES6 Proxies.

Build Coverage Status JavaScript Style Guide Package size Version dependencies Status License

Browser support

Breaking change in v6: the default bundle changed from the ES5 version to the ES6 version. If you experience problems during the build process, please check this docs section.

Table of Contents

Introduction

Easy State has two rules.

  1. Always wrap your components with view.
  2. Always wrap your state store objects with store.
import React from 'react'
import { store, view } from 'react-easy-state'

const clock = store({ time: new Date() })
setInterval(() => (clock.time = new Date()), 1000)

export default view(() => <div>{clock.time.toString()}</div>)

This is enough for it to automatically update your views when needed - no matter how exotically you mutate your state stores. With this freedom you can invent and use your personal favorite state management patterns.

Installation

npm install react-easy-state

Setting up a quick project

Easy State supports Create React App without additional configuration. Just run the following commands to get started.

npx create-react-app my-app
cd my-app
npm install react-easy-state
npm start

You need npm 5.2+ to use npx.

Usage

Creating stores

store creates a state store from the passed object and returns it. State stores are just like normal JS objects. (To be precise, they are transparent reactive proxies of the original object.)

import { store } from 'react-easy-state'

const user = store({
  name: 'Rick'
})

// stores behave like normal JS objects
user.name = 'Bob'
State stores may have arbitrary structure and they may be mutated in any syntactically valid way.
import { store } from 'react-easy-state'

// stores can include any valid JS structure (nested data, arrays, getters, Sets, ...)
const user = store({
  profile: {
    firstName: 'Bob',
    lastName: 'Smith',
    get name () {
      return `${user.firstName} ${user.lastName}`
    }  
  }
  hobbies: ['programming', 'sports']
})

// stores can be mutated in any syntactically valid way
user.profile.firstName = 'Bob'
delete user.profile.lastName
user.hobbies.push('reading')

Creating reactive views

Wrapping your components with view turns them into reactive views. A reactive view re-renders whenever a piece of store - used inside its render - changes.

import React, { Component } from 'react'
import { view, store } from 'react-easy-state'

const user = store({ name: 'Bob' })

class HelloComp extends Component {
  onChange = ev => (user.name = ev.target.value)

  // the render is triggered whenever user.name changes
  render() {
    return (
      <div>
        <input value={user.name} onChange={this.onChange} />
        <div>Hello {user.name}!</div>
      </div>
    )
  }
}

// the component must be wrapped with `view`
export default view(HelloComp)
A single reactive component may use multiple stores inside its render.
import React, { Component } from 'react'
import { view, store } from 'react-easy-state'

const user = store({ name: 'Bob' })
const timeline = store({ posts: ['react-easy-state'] })

class App extends Component {
  onChange = ev => (user.name = ev.target.value)

  // render is triggered whenever user.name or timeline.posts[0] changes
  render() {
    return (
      <div>
        <div>Hello {user.name}!</div>
        <div>Your first post is: {timeline.posts[0]}</div>
      </div>
    )
  }
}

// the component must be wrapped with `view`
export default view(App)

Make sure to wrap all of your components with view - including stateful and stateless ones. If you do not wrap a component, it will not properly render on store mutations.

Creating local stores

A singleton global store is perfect for something like the current user, but sometimes having local component states is a better fit. Just create a store as a component property in these cases.

import React, { Component } from 'react'
import { view, store } from 'react-easy-state'

class ClockComp extends Component {
  clock = store({ time: new Date() })

  componentDidMount() {
    setInterval(() => (this.clock.time = new Date()), 1000)
  }

  render() {
    return <div>{this.clock.time}</div>
  }
}

export default view(ClockComp)

That's it, You know everything to master React state management! Check some of the examples and articles for more inspiration or the FAQ section for common issues.

Examples with live demos

Beginner

Advanced

Articles

FAQ and Gotchas

What triggers a re-render?

Easy State monitors which store properties are used inside each component's render method. If a store property changes, the relevant renders are automatically triggered. You can do anything with stores without worrying about edge cases. Use nested properties, computed properties with getters/setters, dynamic properties, arrays, ES6 collections and prototypal inheritance - as a few examples. Easy State will monitor all state mutations and trigger renders when needed. (Big cheer for ES6 Proxies!)

My component renders multiple times unnecessarily

Re-renders are batched 99% percent of the time until the end of the state changing function. You can mutate your state stores multiple times in event handlers, async functions and timer and networking callbacks without worrying about multiple renders and performance.

If you mutate your stores multiple times synchronously from exotic task sources, multiple renders may happen though. In these rare occasions you can batch changes manually with the batch function. batch(fn) executes the passed function immediately and batches any subsequent re-renders until the function execution finishes.

import React from 'react'
import { view, store, batch } from 'react-easy-state'

const user = store({ name: 'Bob', age: 30 })

// this makes sure the state changes will cause maximum one re-render,
// no matter where this function is getting invoked from
function mutateUser() {
  batch(() => {
    user.name = 'Ann'
    user.age = 32
  })
}

export default view(() => (
  <div>
    name: {user.name}, age: {user.age}
  </div>
))

NOTE: The React team plans to improve render batching in the future. The batch function and built-in batching may be deprecated and removed in the future in favor of React's own batching.

How do I derive local stores from props (getDerivedStateFromProps)?

Components wrapped with view have an extra static deriveStoresFromProps lifecycle method, which works similarly to the vanilla getDerivedStateFromProps.

import React, { Component } from 'react'
import { view, store } from 'react-easy-state'

class NameCard extends Component {
  userStore = store({ name: 'Bob' })

  static deriveStoresFromProps(props, userStore) {
    userStore.name = props.name || userStore.name
  }

  render() {
    return <div>{this.userStore.name}</div>
  }
}

export default view(NameCard)

Instead of returning an object, you should directly mutate the passed in stores. If you have multiple local stores on a single component, they are all passed as arguments - in their definition order - after the first props argument.

My store methods are broken

You should avoid using the this keyword in the methods of your state stores.

const counter = store({
  num: 0,
  increment() {
    this.num++
  }
})

export default view(() => <div onClick={counter.increment}>{counter.num}</div>)

The above snippet won't work, because increment is passed as a callback and loses its this. You should use the direct object reference - counter in this case - instead of this.

const counter = store({
  num: 0,
  increment() {
    counter.num++
  }
})

This works as expected, even when you pass store methods as callbacks.

My views are not rendering

You should wrap your state stores with store as early as possible to make them reactive.

const person = { name: 'Bob' }
person.name = 'Ann'

export default store(person)

The above example wouldn't trigger re-renders on the person.name = 'Ann' mutation, because it is targeted at the raw object. Mutating the raw - none store wrapped object - won't schedule renders.

Do this instead of the above code.

const person = store({ name: 'Bob' })
person.name = 'Ann'

export default person

Naming local stores as state

Naming your local state stores as state may conflict with React linter rules, which guard against direct state mutations. Please use a more descriptive name instead.

Platform support

  • Node: 6 and above
  • Chrome: 49 and above
  • Firefox: 38 and above
  • Safari: 10 and above
  • Edge: 12 and above
  • Opera: 36 and above
  • React Native: iOS 10 and above and Android with community JSC
  • IE is not supported and never will be

This library is based on non polyfillable ES6 Proxies. Because of this, it will never support IE.

React Native is supported on iOS and Android is supported with the community JavaScriptCore. Learn how to set it up here. It is pretty simple.

Performance

You can compare Easy State with plain React and other state management libraries with the below benchmarks. It performs a bit better than MobX and a bit worse than Redux.

How does it work?

Under the hood Easy State uses the @nx-js/observer-util library, which relies on ES6 Proxies to observe state changes. This blog post gives a little sneak peek under the hood of the observer-util.

Alternative builds

This library detects if you use ES6 or commonJS modules and serve the right format to you. The default bundles use ES6 features, which may not yet be supported by some minifier tools. If you experience issues during the build process, you can switch to one of the ES5 builds from below.

  • react-easy-state/dist/es.es6.js exposes an ES6 build with ES6 modules.
  • react-easy-state/dist/es.es5.js exposes an ES5 build with ES6 modules.
  • react-easy-state/dist/cjs.es6.js exposes an ES6 build with commonJS modules.
  • react-easy-state/dist/cjs.es5.js exposes an ES5 build with commonJS modules.

If you use a bundler, set up an alias for react-easy-state to point to your desired build. You can learn how to do it with webpack here and with rollup here.

Contributing

Contributions are always welcome. Just send a PR against the master branch or open a new issue. Please make sure that the tests and the linter pass and the coverage remains decent. Thanks!

Load More Related Articles
Load More By john
Load More In Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Building a Stable Node JS Architecture: Tips and Techniques

Often product development process which involves JavaScript, is accompanied by the use of …