why dose useLocalStorage use custom event?
jakkku opened this issue · comments
Hi, first of all, thank you for providing a nice library.
I love this and am learning a lot
I have some questions.
In your code, The useLocalStorage
hook uses a custom event internally.
However, custom events could be triggered by not only internal function, but also other external functions anywhere.
I thought using local scope variable is more safe.
but if you have some reason using CustomEvent
, could i ask??
// This is an example of a third-party store
// that you might need to integrate with React.
// If your app is fully built with React,
// we recommend using React state instead.
let nextId = 0;
let todos = [{ id: nextId++, text: 'Todo #1' }];
let listeners = [];
export const todosStore = {
addTodo() {
todos = [...todos, { id: nextId++, text: 'Todo #' + nextId }]
emitChange();
},
subscribe(listener) {
listeners = [...listeners, listener];
return () => {
listeners = listeners.filter(l => l !== listener);
};
},
getSnapshot() {
return todos;
}
};
function emitChange() {
for (let listener of listeners) {
listener();
}
}