Reman
A easy & pocket State Handler with React Context instead of Redux 😆😆😆
Getting Started
npm install reman # or yarn add reman
First of all, Reman is built with react hooks in react@16.8, it works wonderfully for the Function Component
, but not very good for Class Component
. However, Reman.connect will help you to work with Class Component
.
In Reman, we use context
to replace store
in redux, and make up reducers just like rematch, in the same time, you should organize business data with serveral contexts instead of the only one store.
Step 1: Create context
A little like Rematch, the Context brings together state, reducers, middlewares in one place. You just create context, and it works automatically.
Step 2: Use Provider
Provider should be your app's root element, it make sure your Function Component
can respond to the change of state in context, also for multi-contexts.
render , document.getElementById'root';
Step 3: Dispatch action
You should use useContext
to get state & dispatch from the Context you created.
Advance
Connect
The connect
API help you easily to build up a new hook for UI component:
;
Not like the connect
in react-redux, generally used to map data/action
from Store to Component, the connect
in Reman should be applied in two cases:
- Need
useContext
, or even other hooks for a Class Component - Require better performance of component rendering, without multiple & simple contexts
Yead, connect
of Reman use React.memo
to improve the rendering performance of generated component.
In the example above, When yourContext
change, the component will not render until the fields data
& action
change, which is declared by the function in the connect
call.
In the beginning, with function components, you don't need connect
until touch a rendering performance problem.
The API Named
connect
make you feel like useing react-redux, and you know what to do with it.
Middleware
Emm, use it just like middleware in redux
:
- Middlewares for one context:
;
- Middlewares for all context:
; applyGlobalMiddleware;
Helper
Now there is only one helper in Reman: merge
, to help you merge several function to one which can be used in connect
.
;
Example
Check example
dir for more details about useing.
Or you can look for some using tips in testcases.
Development
First, clone repo and use yarn
to install deps for testing.
yarn test # test
Thanks
TODO
- test
- middleware
- connect
- devtool
License
Reman is published under the MIT license. See LICENSE for more information.