sdorex
v1.0.9
Published
probably the most minimalist state management
Downloads
6
Maintainers
Readme
sdorex
probably the most minimalist state management in react
- only 1 API
- reactive, no unnecessary re-render
- easy, intuit GET and SET state
Install
npm i sdorex
# OR
yarn add sdorex
Usage
basic usage
import sdorex from "sdorex";
// init store
const store = sdorex({ count: 0 });
export default function App() {
return (
<>
<p>{store.count}</p>
<button onClick={() => store.count++}>ADD</button>
</>
);
}
define function
const store = sdorex({
count: 0,
inc: () => store.count++,
});
export default function App() {
return (
<>
<p>{store.count}</p>
<button onClick={() => store.inc()}>ADD</button>
</>
);
}
reactive
count
change but User
will not re-render
const store = sdorex({
count: 0,
user: "Tom",
});
function Count() {
console.log("Count render.");
return <p>{store.count}</p>;
}
function User() {
console.log("User render.");
return <p>{store.user}</p>;
}
export default function App() {
return (
<>
<Count />
<User />
<button onClick={() => store.count++}>ADD</button>
</>
);
}
define object
const store = sdorex({
user: {
name: "Tom",
},
});
function User() {
return (
<>
<p>{store.user.name}</p>
<p>{store.user.gender}</p>
</>
);
}
export default function App() {
return (
<>
<User />
<button
onClick={() => {
store.user.name = "Jerry";
store.user.gender = "girl";
}}
>
GO
</button>
</>
);
}