Skip to content

Instantly share code, notes, and snippets.

@jlengstorf
Created May 30, 2025 02:00
Show Gist options
  • Select an option

  • Save jlengstorf/dfe85362ee214ea6117791cf6afe8dbc to your computer and use it in GitHub Desktop.

Select an option

Save jlengstorf/dfe85362ee214ea6117791cf6afe8dbc to your computer and use it in GitHub Desktop.
this is not real code, but it's pretty similar to what I was trying when I got frustrated with trying to write React hooked up to a non-React lib
// external thing that sends/receives MIDI
const box = new Box();
export function MyComponent() {
const [code, setCode] = useState('');
useEffect(() => {
function handleKeyPress(id, key) {
setState((curr) => curr += 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);
}, []);
// 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