-
-
Save gaearon/6f644154c3c65774d64d4c7e84c966e7 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
| // external thing that sends/receives MIDI | |
| const box = new Box(); | |
| export function MyComponent() { | |
| const [code, setCode] = useState(''); | |
| // ... | |
| useEffect(() => { | |
| function handleKeyPress(id, key) { | |
| // ... | |
| } | |
| async function handleEnterKey(id) { | |
| // hits an external DB | |
| // for some reason this was always showing code as an empty string | |
| const isValid = await checkForValidCode(code); | |
| if (isValid) { | |
| box.setSuccessOn(id); | |
| } else { | |
| box.setFailureOn(id); | |
| } | |
| setCode(''); | |
| } | |
| box.onKey(handleKeyPress); | |
| box.onEnter(handleEnterKey); | |
| return () => { | |
| box.offKey(handleKeyPress); | |
| box.offEnter(handleEnterKey); | |
| }; | |
| }, [code]); | |
| // the UI is also subscribed to some other stuff to do with code claim status, etc. | |
| return ( | |
| <p>{code}</p> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment