React Easy State
Simple React state management. Made with ❤️ and ES6 Proxies.
Table of Contents
NEWS: v6.1.2 fixed a nasty memory leak. Please upgrade Easy State to v6.1.2 if you use a v6.x version already. Thanks!
Introduction
React Easy State is a practical state management library with two functions and two accompanying rules.
- Always wrap your components with
view()
. - Always wrap your state store objects with
store()
.
import React from 'react'import store view from 'react-easy-state' const counter = const increment = counternum++ <button =>counternum</button>
This is enough for it to automatically update your views when needed. It doesn't matter how you structure or mutate your state stores, any syntactically valid code works.
Check this TodoMVC codesandbox or raw code for a more exciting example with nested data, arrays and computed values.
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-appcd my-appnpm install react-easy-statenpm start
You need npm 5.2+ to use npx.
Usage
Creating global stores
store
creates a state store from the passed object and returns it. A state store behaves just like the passed object. (To be precise, it is a transparent reactive proxy of the original object.)
const user = // stores behave like normal JS objectsusername = 'Bob'
State stores may have arbitrary structure and they may be mutated in any syntactically valid way.
// stores can include any valid JS structure// including nested data, arrays, Maps, Sets, getters, setters, inheritance, ...const user = // stores may be mutated in any syntactically valid wayuserprofilefirstName = 'Bob'delete userprofilelastNameuserhobbiesuserfriends
Async operations can be expressed with the standard async/await syntax.
const userStore =
State stores may import and use other state stores in their methods.
userStore.js
const userStore =
recipesStore.js
const recipesStore =
Wrap your state stores with store
as early as possible.
// DON'T DO THISconst person = name: 'Bob' personname = 'Ann' person
// DO THIS INSTEADconst person = personname = 'Ann'
The first 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.
Avoid using the this
keyword in the methods of your state stores.
import store view from 'react-easy-state' const counter = <div =>counternum</div>
this.num++
won't work, because increment
is passed as a callback and loses its this
. You should use the direct object reference - counter
- instead of this
.
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 from 'react'import view store from 'react-easy-state' // this is a global state storeconst user = // this is re-rendered whenever user.name changes <div> <input = = /> <div>Hello username!</div> </div>
Wrap ALL of your components with view
- including class and function ones - even if they don't seem to directly use a store.
import view store from 'react-easy-state' const appStore = const App = // DO THISconst Profile = // DON'T DO THIS// This won't re-render on appStore.user.name = 'newName' like mutationsconst Profile = user <p>Name: username</p>
A single reactive component may use multiple stores inside its render.
import React from 'react'import view store from 'react-easy-state' const user = const timeline = // this is re-rendered whenever user.name or timeline.posts[0] changes <div> <div>Hello username!</div> <div>Your first post is: timelineposts0</div> </div>
view
implements an optimal shouldComponentUpdate
(or memo
) for your components.
-
Using
PureComponent
ormemo
will provide no additional performance benefits. -
Defining a custom
shouldComponentUpdate
may rarely provide performance benefits when you apply some use case specific heuristics inside it.
Reactive renders are batched. Multiple synchronous store mutations won't result in multiple re-renders of the same component.
import React from 'react'import view store batch from 'react-easy-state' const user = { username = 'Ann' userage = 32} // calling `mutateUser` will only trigger a single re-render of the below component// even though it mutates the store two times in quick succession <div => name: username age: userage </div>
If you mutate your stores multiple times synchronously from exotic task sources, multiple renders may rarely happen. If you experience performance issues 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 = { // this makes sure the state changes will cause maximum one re-render, // no matter where this function is getting invoked from } <div> name: username age: userage </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.
Always apply view
as the latest (innermost) wrapper when you combine it with other Higher Order Components.
import view from 'react-easy-state'import withRouter from 'react-router-dom'import withTheme from 'styled-components' const Comp = <div>A reactive component</div> // DO THIS // DON'T DO THIS
Usage with (pre v4.4) React Router.
-
If routing is not updated properly, wrap your
view(Comp)
- with theRoute
s inside - inwithRouter(view(Comp))
. This lets react-router know when to update. -
The order of the HOCs matter, always use
withRouter(view(Comp))
.
This is not necessary if you use React Router 4.4+. You can find more details and some reasoning about this in this react-router docs page.
Passing nested data to third party components.
Third party helpers - like data grids - may consist of many internal components which can not be wrapped by view
, but sometimes you would like them to re-render when the passed data mutates. Traditional React components re-render when their props change by reference, so mutating the passed reactive data won't work in these cases. You can solve this issue by deep cloning the observable data before passing it to the component. This creates a new reference for the consuming component on every store mutation.
import React from 'react'import view store from 'react-easy-state'import Table from 'rc-table'import cloneDeep from 'lodash/cloneDeep' const dataStore = <Table = />
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 inside a function component or as a class component property in these cases.
Local stores in function components
import React from 'react'import view store from 'react-easy-state' const counter = const increment = counternum++ return <button=>counternum</div>
Local stores in functions rely on React hooks. They require React and React DOM v16.8+ or React Native v0.59+ to work.
You can use any React hook - including useState
- in function components, Easy State won't interfere with them.
import React from 'react'import view store from 'react-easy-state' const name setName = const user = return <div> <input = = /> <input = = /> </div>
Local stores in class components
import React Component from 'react'import view store from 'react-easy-state' counter = counternum++ { return <button =>thiscounternum</button> } Counter
You can also use vanilla setState
in your class components, Easy State won't interfere with it.
import React Component from 'react'import view store from 'react-easy-state' state = name: 'Ann' user = this thisuserage = evtargetvalue { return <div> <input = = /> <input = = /> </div> } Profile
Don't name local stores as state
. It may conflict with linter rules, which guard against direct state mutations.
import React Component from 'react'import view store from 'react-easy-state' // DON'T DO THIS state = // DO THIS user = {}
Deriving local stores from props (getDerivedStateFromProps
).
Class 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' userStore = static { userStorename = propsname || userStorename } { return <div>thisuserStorename</div> } NameCard
Instead of returning an object, you should directly mutate the received 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.
Examples with live demos
Beginner
- Clock Widget (source) (codesandbox) (react-native source) (react-native sandbox): a reusable clock widget with a tiny local state store.
- Stopwatch (source) (codesandbox) (tutorial): a stopwatch with a mix of normal and computed state properties.
Advanced
- TodoMVC (source) (codesandbox): a classic TodoMVC implementation with a lot of computed data and implicit reactivity.
- Contacts Table (source) (codesandbox): a data grid implementation with a mix of global and local state.
- Beer Finder (source) (codesandbox) (tutorial): an app with async actions and a mix of local and global state, which finds matching beers for your meal.
Articles
- Introducing React Easy State: making a simple stopwatch.
- Stress Testing React Easy State: demonstrating Easy State's reactivity with increasingly exotic state mutations.
- Design Patterns with React Easy State: demonstrating async actions and local and global state management through a beer finder app.
- The Ideas Behind React Easy State: a deep dive under the hood of Easy State.
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 similarly to Redux.
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: 0.59 and above
This library is based on non polyfillable ES6 Proxies. Because of this, it will never support IE.
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!