I'm opening this issue so we can discuss how Ink could take advantage of React Hooks.
For example, maybe useStdin() instead of <StdinContext>?
rough draft
const useKeyHandler = keyHandler => { const { stdin, setRawMode } = useContext(StdinContext) useEffect(() => { setRawMode(true) stdin.on('data', keyHandler) return () => { stdin.off('data', keyHandler) setRawMode(false) } }, [stdin, setRawMode]) }
useKeyHandler(data => { if (data === 's') setSyncing(syncing => !syncing) if (data === 'p') setCreature(ascii()) if (data === 'q') process.exit(0) })
@jedahan Let's keep the keypress discussion to #138.
What would useStdin expose ideally, beyond something like const { stdin, setRawMode } = useContext(StdinContext) ?
In 2.5.0 useApp, useStdin and useStdout hooks were added.
I have a useExit hook for a project I'd be willing to upstream:
import { useEffect } from 'react';
import { useApp } from 'ink';
export class ExitError extends Error {
public code: number;
constructor(code: number, message?: string) {
super(message);
this.code = code;
}
}
export const useExit = (error?: Error) => {
const { exit } = useApp();
useEffect(() => {
exit(error);
}, [exit]);
};
Don't know if the ExitError class is something you want or not, but it enables the code that runs the CLI to set the process's exit code.
Happy to PR this if you want it.
I don't think there's a benefit to having useExit when useApp exposes the same functionality via exit function.
Fair. This issue might be worth closing now that the first batch is in.
Most helpful comment
In
2.5.0useApp,useStdinanduseStdouthooks were added.