
A lightweight state container based on Redux
Read the intro blog post
{count}
Table of Contents
Installation
To install the stable version:
npm i redux-zero
This assumes that you’re using npm with a module bundler like webpack
How
ES2015+:
;;
TypeScript:
;;
CommonJS:
const createStore = ;const Provider connect = ;
UMD:
<!-- the store --> <!-- for react --> <!-- for preact --> <!-- for vue --> <!-- for svelte -->
Example
Let's make an increment/decrement simple application with React:
First, create your store. This is where your application state will live:
/* store.js */; const initialState = count: 1 ;const store = ; ;
Then, create your actions. This is where you change the state from your store:
/* actions.js */const actions = count: statecount + 1 count: statecount - 1 ; ;
By the way, because the actions are bound to the store, they are just pure functions :)
Now create your component. With Redux Zero your component can focus 100% on the UI and just call the actions that will automatically update the state:
/* Counter.js */;; ; const mapToProps = count ; mapToProps actions <div> <h1>count</h1> <div> <button onClick=decrement>decrement</button> <button onClick=increment>increment</button> </div> </div>;
Last but not least, plug the whole thing in your index file:
/* index.js */;;; ; ; const App = <Provider store=store> <Counter /> </Provider>; ;
Here's the full version: https://codesandbox.io/s/n5orzr5mxj
By the way, you can also reset the state of the store anytime by simply doing this:
; store;
More examples
Actions
There are three gotchas with Redux Zero's actions:
- Passing arguments
- Combining actions
- Binding actions outside your application scope
Passing arguments
Here's how you can pass arguments to actions:
const Component = <h1 onClick= >count</h1>; const mapToProps = count ; const actions = count: statecount + value ; const ConnectedComponent = Component; const App = <Provider store=store> <ConnectedComponent /> </Provider>;
Access props in actions
The initial component props are passed to the actions creator.
const Component = <h1 onClick= >count</h1>; const mapToProps = count ; const actions = count: statecount + ownPropsvalue ; const ConnectedComponent = Component; const App = <Provider store=store> <ConnectedComponent value=10 /> </Provider>;
Combining actions
There's an utility function to combine actions on Redux Zero:
;; ;;; params moreParams Component;
Binding actions outside your application scope
If you need to bind the actions to an external listener outside the application scope, here's a simple way to do it:
On this example we listen to push notifications that sends data to our React Native app.
;;;; const messaging = firebase;const boundActions = ; messaging;
Async
Async actions in Redux Zero are almost as simple as sync ones. Here's an example:
const mapActions = { ; return client ; };
They're still pure functions. You'll need to invoke setState
if you have a loading status. But at the end, it's the same, just return whatever the updated state that you want.
And here's how easy it is to test this:
;
Middleware
The method signature for the middleware was inspired by redux. The main difference is that action is just a function:
/* store.js */;; const logger = { console; console; return ;}; const initialState = count: 1 ;const middlewares = ; const store = ; ;
DevTools
You can setup DevTools middleware in store.js to connect with Redux DevTools and inspect states in the store.
/* store.js */;;; const initialState = count: 1 ;const middlewares = connect ? : ;const store = ; ;
Also, these are unofficial tools, maintained by the community:
- Redux-Zero Tools
- redux-zero persist middleware
- redux-zero logger middleware
- redux loading middleware
TypeScript
You can use the BoundActions
type to write your React component props in a type
safe way. Example:
; ; </h1>); ; ; ;
By doing this, TypeScript will know the available actions and their types
available on the component's props. For example, you will get a compiler error if you
call props.setLoding
(that action doesn't exist), or if you call it
with incorrect argument types, like props.setLoading(123)
.
Inspiration
Redux Zero was based on this gist by @developit
Roadmap
- Make sure all bindings are working for latest versions of React, Vue, Preact and Svelte
- Add time travel
Help is needed for both of these