React Vessel
A different approach for managing your state in react. Build your applications with composing dynamic reducers through JSX api.
🕹 CodeSandbox demos 🕹 |
---|
Counter |
Installation
Install react-vessel:
npm install react-vessel
or with yarn:
yarn add react-vessel
What you can do with it
You can write a simple counter like this
Here is the simplest thing you can do with a vessel.
import React from 'react';import Vessel Reducer from 'react-vessel'; { const dispatch = ; const count = ; return <div> <Reducer = ="increment" = /> <Reducer = ="decrement" = /> <div>count</div> <button ="button" => Increment </button> <button ="button" => Decrement </button> </div> ;} { return <Vessel> <Counter ="counter1" /> <Counter ="counter2" /> </Vessel> ;}
Notice how you could easily reuse the component.
Above code will produce following state in your vessel:
You can access this state anywhere in your application
{ const count = ; return <div>count</div>;}
Or you can use a component with render props if you prefer that:
{ return <WithVessel ="counter1" =>}
You can add/remove reducers dynamically
{ const incrementEnabled setIncrementEnabled = ; return <React.Fragment> incrementEnabled && <Reducer ="my-counter" ="increment" = /> <Reducer ="my-counter" ="decrement" = /> <button ="button" => Enable/Disable Increment </button> </React.Fragment> ;}
You can combine multiple reducers inside a Model
<Model ="my-counter"> <Reducer ="increment" = /> <Reducer ="decrement" = /></Model>
You can build a simple FormInput
{ const value = ; const dispatch = ; { ; } return <React.Fragment> <Reducer = ="change" = /> </React.Fragment> ;} { return <FormInput = = /> ;} { return <Vessel> <TextInput ="smart-input" /> <WithVessel ="input.value" = /> </Vessel> ;}
You can write effects that runs after an action
{ const dispatch = ; return <Reducer ="username" ="validate-suc" = /> <Reducer ="username" ="validate-err" = /> <Effect ="username/changed" = /> ;} { return <Vessel> <TextInput ="username" /> <ValidateUsername /> </Vessel> ;}