Plz, What's best store design pattern in this case?

I made a simple example code to explain my current situation. Please see my picture. I have color property as global, also in pages. I made getter and setter to change page color. But if global color is active, The global color should be changed, also should be returned by getter. Is there other way than better than this? Now I have only color property, so making setter and getter is not hard. but I will have much more properties which also has same global properties... Creating each property setter & getter is really tedious in this case..
3 Replies
Alex Lohr
Alex Lohr3y ago
You can use a HOF to update each property either on the page or globally:
const setItem = (name, value) => state[`isGlobal${name[0].toUpperCase()}${name.slice(1)}Active]
? setState(name, value)
: setState('pages', index, name, value);
const setItem = (name, value) => state[`isGlobal${name[0].toUpperCase()}${name.slice(1)}Active]
? setState(name, value)
: setState('pages', index, name, value);
And a similar function for the getter.
musiclover
musicloverOP3y ago
Thanks Alex. It's amazing solution I was look for.... !!! What's is HOF? is it kind of pattern name?
Alex Lohr
Alex Lohr3y ago
A higher order function (one that returns a function), but I found that pattern, that will do the trick, too
Want results from more Discord servers?
Add your server