Last active
September 17, 2019 07:52
-
-
Save andregardi/04c4af602c008fafc10f38b32c7cb203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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