Ink: React Hooks

Created on 7 Mar 2019  路  7Comments  路  Source: vadimdemedes/ink

I'm opening this issue so we can discuss how Ink could take advantage of React Hooks.

For example, maybe useStdin() instead of <StdinContext>?

question

Most helpful comment

In 2.5.0 useApp, useStdin and useStdout hooks were added.

All 7 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HMR
konsumer picture konsumer  路  3Comments

zkat picture zkat  路  7Comments

cometkim picture cometkim  路  3Comments

gc picture gc  路  6Comments

isaacs picture isaacs  路  4Comments