Skip to content

Instantly share code, notes, and snippets.

@andregardi
Last active September 17, 2019 07:52
Show Gist options
  • Select an option

  • Save andregardi/04c4af602c008fafc10f38b32c7cb203 to your computer and use it in GitHub Desktop.

Select an option

Save andregardi/04c4af602c008fafc10f38b32c7cb203 to your computer and use it in GitHub Desktop.
import { useState, useEffect } from 'react';
let listeners = [];
let state = { counter: 0 };
const setState = (newState) => {
state = { ...state, ...newState };
listeners.forEach((listener) => {
listener(state);
});
};
const useCustom = () => {
const newListener = useState()[1];
useEffect(() => {
listeners.push(newListener);
}, []);
return [state, setState];
};
export default useCustom;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment