hookex
A state manager for React without reducer, Provider, dispatcher etc. Design for large projects which has thousand of components.
- No Provider needed
- No Store needed
- No Reducer needed
- No Action Creator needed
- No Dispatcher needed
- Simple concept: State & Action
- Support Simple State (Synchronous State)
- Support Asynchronous State (with debouncing)
- Support Dynamic State
Samples
Counter App
import React from "react";import render from "react-dom";import createState createAction useStates from "hookex"; // define CountState with 1 as default valueconst CountState = ;// define an action, specified CountState as dependencies// action body receives CountState accessor// using count() to get current state value and count(newValue) to update stateconst Increase = ; { const count = ; return <div> Counter: count <button =>Click to increase counter</button> </div> ;} ;
Dynamic State
Create dynamic state which is computed from other states
import React from "react";import render from "react-dom";import createState createAction useStates from "hookex"; const CountState = ;const DoubleCountState = ;const Increase = ; { const count doubleCount = ; return <div> <p>Counter: count</p> <p>Double Counter: doubleCount</p> <button =>Click to increase counter</button> </div> ;} ;
Async State
Search github user
import React from "react";import render from "react-dom";import createState createAction useStates from "hookex"; const apiUrl = "https://api.github.com/users/";const SearchTermState = ;const UpdateSearchTerm = ;// once searchTerm changed, UserInfo state will be recomputedconst UserInfoState = ; { const searchTerm userInfo = ; const value done = userInfo; return <div> <input ="text" = = /> <pre>done ? JSON : "Searching..."</pre> </div> ;} ;
Using AsyncRender component
AsyncRender component receives specified async state (or multiple states). When state loaded, render callback/component will be called unless AsyncRender's children will be rendered instead
import React from "react";import render from "react-dom";import createState createAction useStates AsyncRender from "./hookex"; const apiUrl = "https://api.github.com/users/";const SearchTermState = ;const UpdateSearchTerm = ;const UserInfoState = ; { return <pre>JSON</pre>;} { const searchTerm = ; return <div> <p> <input ="text" = = /> </p> <AsyncRender = => Loading... </AsyncRender> </div> ;} ;
Saving and loading states with localStorage
import createState createAction persist from "hookex"; const CounterState = ;const Increase = ; ; ;
Update single state
Note: Cannot update computed state
import createState from "hookex"; const CounterState = ; ;
Using State as event handler
You can pass state to element event, it can process input synthetic event (event.target.value/event.target.checked)
import React from "react";import render from "react-dom";import createState useStates from "hookex"; const ValueState = ;const CheckedState = ; { const value checked = ; return <> <p> <input = = /> </p> <p>value</p> <p> <input ="checkbox" = = /> </p> <p>checked ? "checked" : "unchecked"</p> </> ;} ;