Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Hooks #137

Closed
sindresorhus opened this issue Mar 7, 2019 · 7 comments
Closed

React Hooks #137

sindresorhus opened this issue Mar 7, 2019 · 7 comments
Labels

Comments

@sindresorhus
Copy link
Collaborator

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

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

@jedahan
Copy link

jedahan commented Mar 7, 2019

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) })

@sindresorhus
Copy link
Collaborator Author

sindresorhus commented Mar 7, 2019

@jedahan Let's keep the keypress discussion to #138.

@jedahan
Copy link

jedahan commented Mar 7, 2019

What would useStdin expose ideally, beyond something like const { stdin, setRawMode } = useContext(StdinContext) ?

@vadimdemedes
Copy link
Owner

In 2.5.0 useApp, useStdin and useStdout hooks were added.

@mAAdhaTTah
Copy link
Contributor

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.

@vadimdemedes
Copy link
Owner

I don't think there's a benefit to having useExit when useApp exposes the same functionality via exit function.

@mAAdhaTTah
Copy link
Contributor

Fair. This issue might be worth closing now that the first batch is in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants