当前位置:首页 > React

如何使用ink react

2026-03-30 19:54:11React

安装 Ink React

确保系统已安装 Node.js(建议版本 12 或更高)。通过 npm 或 yarn 安装 inkreact 依赖包:

npm install ink react

yarn add ink react

创建基础组件

Ink 允许使用 React 语法编写命令行界面。创建一个简单的组件文件(如 cli.js):

import React from 'react';
import { render, Text } from 'ink';

const App = () => <Text color="green">Hello, Ink!</Text>;

render(<App />);

运行组件

通过 Node.js 执行组件文件:

node cli.js

终端将显示绿色文本 "Hello, Ink!"。

添加交互功能

Ink 支持用户输入和动态更新。使用 useStateTextInput 组件实现输入交互:

如何使用ink react

import React, { useState } from 'react';
import { render, Text, TextInput } from 'ink';

const App = () => {
  const [input, setInput] = useState('');
  return (
    <>
      <Text>Enter your name: </Text>
      <TextInput value={input} onChange={setInput} />
      <Text>You typed: {input}</Text>
    </>
  );
};

render(<App />);

处理命令行参数

通过 process.argv 或第三方库(如 yargs)获取命令行参数:

import React from 'react';
import { render, Text } from 'ink';

const App = () => <Text>Args: {process.argv.join(' ')}</Text>;
render(<App />);

使用布局组件

Ink 提供 BoxNewline 等布局组件控制输出格式:

import { render, Box, Text, Newline } from 'ink';

const App = () => (
  <Box flexDirection="column">
    <Text>First line</Text>
    <Newline />
    <Text color="yellow">Second line</Text>
  </Box>
);

render(<App />);

生命周期管理

通过 useEffect 处理组件生命周期,如定时更新:

如何使用ink react

import React, { useState, useEffect } from 'react';
import { render, Text } from 'ink';

const App = () => {
  const [count, setCount] = useState(0);
  useEffect(() => {
    const timer = setInterval(() => setCount(c => c + 1), 1000);
    return () => clearInterval(timer);
  }, []);
  return <Text>Count: {count}</Text>;
};

render(<App />);

错误边界

使用 ErrorBoundary 捕获子组件错误:

import { render, ErrorBoundary, Text } from 'ink';

const BuggyComponent = () => { throw new Error('Crash!'); };

const App = () => (
  <ErrorBoundary fallback={({ error }) => <Text color="red">{error.message}</Text>}>
    <BuggyComponent />
  </ErrorBoundary>
);

render(<App />);

测试组件

Ink 支持通过 ink-testing-library 进行单元测试:

npm install --save-dev ink-testing-library

测试示例:

import { render } from 'ink-testing-library';
import { App } from './cli';

test('displays greeting', () => {
  const { lastFrame } = render(<App />);
  expect(lastFrame()).toContain('Hello, Ink!');
});

打包发布

使用 pkgnexe 将应用打包为可执行文件:

npm install -g pkg
pkg cli.js --targets node14-linux-x64

生成的可执行文件可直接运行无需 Node.js 环境。

标签: 如何使用ink
分享给朋友:

相关文章

uniapp中如何使用iconfont

uniapp中如何使用iconfont

使用 Iconfont 在 Uniapp 中的步骤 下载 Iconfont 资源 访问 Iconfont 官网,选择需要的图标并添加到项目。下载时选择 Font class 格式,解压后会得到 .tt…

react如何使用redux

react如何使用redux

使用 Redux 在 React 中的应用 Redux 是一个状态管理库,通常与 React 结合使用以管理全局状态。以下是具体实现步骤: 安装依赖 确保项目中已安装 redux 和 react-r…

react如何使用jquery

react如何使用jquery

在 React 中使用 jQuery React 和 jQuery 的设计理念不同,React 基于虚拟 DOM 和数据驱动,而 jQuery 直接操作真实 DOM。若需在 React 中整合 jQu…

react dnd如何使用

react dnd如何使用

React DnD 使用指南 React DnD(Drag and Drop)是一个用于在 React 应用中实现拖放功能的库。它基于 HTML5 的拖放 API,提供了更高级的抽象和更好的 Reac…

如何使用react native

如何使用react native

安装开发环境 确保系统已安装Node.js(建议LTS版本)。通过npm或yarn全局安装React Native命令行工具: npm install -g react-native-cli # 或…

react如何使用插槽

react如何使用插槽

使用props.children实现基础插槽 React中插槽的核心是通过props.children传递子组件。父组件在标签内部放置的内容会自动成为children属性: // 父组件 <C…