Skip to content

Instantly share code, notes, and snippets.

@raphaelameaume
Created February 1, 2024 13:23
Show Gist options
  • Save raphaelameaume/44683ea40c2761c4d5e3506ce67980de to your computer and use it in GitHub Desktop.
Save raphaelameaume/44683ea40c2761c4d5e3506ce67980de to your computer and use it in GitHub Desktop.
Save/retrieve props with localStorage
function syncProps(props) {
return Object.keys(props).reduce((all, key) => {
const prop = { ...props[key] };
const storageKey = `${key}`;
const { onChange = () => {} } = prop;
prop.onChange = (prop) => {
localStorage.setItem(storageKey, JSON.stringify(prop.value));
onChange(prop);
};
all[key] = prop;
if (localStorage.getItem(storageKey)) {
all[key].value = JSON.parse(localStorage.getItem(storageKey));
}
return all;
}, {});
}
export let props = syncProps({
radius: {
value: 12,
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment