flaze-dev / envy

A simple state management library
0 stars 0 forks source link

[Partial] Option to only update a view when a specific property updates. #6

Open ingoandelhofs opened 2 years ago

ingoandelhofs commented 2 years ago
const name = value({
   firstName: "John",
   lastName: "Doe",
   age: 34
});
// This component will only update when firstName changes
const firstName = usePartial(name, ["firstName"]);
return <div>{firstName}</div>
// Alternative 
const firstName = usePartial(name, (name) => name.firstName);
return <div>{firstName}</div>
// Alternative 
const {fn, ln}= usePartial(name, (name) => {fn: name.firstName, ln: name.lastName});
return <div>{fn} {ln}</div>
ingoandelhofs commented 2 years ago

We can refactor to only have the useValue hook?

// Alternative 
const {fn, ln}= useValue(partial(name, (name) => {fn: name.firstName, ln: name.lastName}));
return <div>{fn} {ln}</div>