Skip to content

Instantly share code, notes, and snippets.

@gaearon
Created May 30, 2025 02:14
Show Gist options
  • Select an option

  • Save gaearon/6f644154c3c65774d64d4c7e84c966e7 to your computer and use it in GitHub Desktop.

Select an option

Save gaearon/6f644154c3c65774d64d4c7e84c966e7 to your computer and use it in GitHub Desktop.
// 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