redux-zero
Redux Zero is a lightweight state container based on Redux with a single store and no reducers, 100% written in TypeScript. Less than 1kb (gzip).
Installation
To install the stable version:
npm install --save redux-zero
This assumes that you’re using npm with a module bundler like webpack
How
ES2015+:
TypeScript:
CommonJS:
UMD:
Example
Let's make an increment/decrement simple application with React:
First, create your store. This is where your application state will live:
Then, create your actions. This is where you change the state from your store:
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:
Last but not least, plug the whole thing in your index file:
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:
More examples
Actions
There are tree 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:
Combining actions
There's an utility function to combine actions on Redux Zero:
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.
Async
Async actions in Redux Zero are almost as simple as sync ones. Here's an example:
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:
DevTools
You can setup DevTools middleware in store.js to connect with Redux DevTools and inspect states in the store.
Also, these are unofficial tools, maintained by the community:
- Redux-Zero Tools
- redux-zero persist middleware
- redux-zero logger middleware
- redux loading middleware