A simple React adapter for using zentropy in React applications. This package provides seamless state updates by subscribing to state changes and triggering re-renders automatically.
- 🌀 Minimal & Lightweight – Just one hook, no extra complexity.
- ⚡ Auto-Updates – Keeps your component in sync with
zentropystate. - 🔄 No Boilerplate – Works with any
zentropystate instance.
npm install zentropy-reactDefine your shared state using zentropy:
import{makeState}from"zentropy";constcounterState=makeState({initial: 0,reducers: {increment: (state)=>state+1,decrement: (state)=>state-1,add: (state,payload)=>state+payload},});exportdefaultcounterState;Subscribe to the state inside a component:
importReactfrom"react";import{useZenState,useZenMiddleware}from"zentropy-react";importcounterStatefrom"./counterState";functionpersistCounter(state,action){// Save your updated state}functionCounter(){const{ value }=useZenState(counterState);useZenMiddleware(counterState,persistCounter);return(<div><p>Count: {value}</p>{/* use reducer actions */}<buttononClick={()=>counterState.actions.increment()}>+</button><buttononClick={()=>counterState.actions.decrement()}>-</button>{/* or use the dispatch functions */}<buttononClick={()=>counterState.dispatch("increment")}>+</button><buttononClick={()=>counterState.dispatch("decrement")}>-</button></div>);}exportdefaultCounter;Zentropy-react also supports watching only a property from a state. This is intended to be used in cases where it makes sense to have an object state but certain components may rely only on part of it to re-render.
importReactfrom"react";import{useZenState}from"zentropy-react";typeFilterState={date?: Date;email?: string;}constinitial: FilterState={}constfiltersState=makeState({
initial
});functionEmailInput(){const{value: email}=useZenState(filtersState,{watch: 'email'});return<>...</>;}functionDateInput(){const{value: date}=useZenState(filtersState,{watch: 'date'});return<>...</>;}functionMyFilters(){const{value: filters}=useZenState(filtersState);return<><EmailInput/><DateInput/></>;}Please notice that using
watchparam will only refresh if the property has changed, so take this in account if you're reading thestateValuereturned
A React hook that subscribes to a zentropy state and keeps components in sync.
constyourState=makeState({initial: {name: 'Eric',online: true}});const{ value }=useZenState(yourState);// value -> { name: 'Eric', online: true }// Or watch only for a property in the stateconst{ value, stateValue }=useZenState(yourState,{watch: 'name'});// value -> 'Eric'// stateValue -> { name: 'Eric', online: true }state– Azentropystate instance.- Returns –
{ value }, wherevalueis the current state.
Hook to add a middleware to listen for changes in the state and run side effects
useZenMiddleware(yourState,(state,action,payload)=>{if(action==='update'){saveToDB(state);}});✅ No Context API required – Just use the hook anywhere.
✅ Automatic reactivity – Renders update only when needed.
✅ Lightweight & Simple – No extra setup, just plug and play.
MIT License.