当前位置:首页 > React

react实现行号

2026-01-26 22:31:00React

实现行号的基本方法

在React中实现行号通常涉及渲染一个带有行号的列表或文本编辑器。以下是几种常见场景的解决方案:

纯文本显示行号

对于简单的文本行号显示,可以使用数组的map方法生成带行号的列表:

const TextWithLineNumbers = ({ text }) => {
  const lines = text.split('\n');

  return (
    <div style={{ fontFamily: 'monospace' }}>
      {lines.map((line, index) => (
        <div key={index}>
          <span style={{ color: 'gray', marginRight: '10px' }}>{index + 1}</span>
          {line}
        </div>
      ))}
    </div>
  );
};

代码编辑器行号实现

对于代码编辑器,可以结合textarea或专业编辑器库实现:

react实现行号

const CodeEditor = () => {
  const [code, setCode] = useState('');
  const lines = code.split('\n');

  return (
    <div style={{ display: 'flex' }}>
      <div style={{ 
        paddingRight: '10px',
        textAlign: 'right',
        userSelect: 'none'
      }}>
        {lines.map((_, i) => (
          <div key={i}>{i + 1}</div>
        ))}
      </div>
      <textarea
        value={code}
        onChange={(e) => setCode(e.target.value)}
        style={{ 
          flex: 1,
          fontFamily: 'monospace',
          height: '300px'
        }}
      />
    </div>
  );
};

使用第三方库

专业场景建议使用现成的代码编辑器组件:

  • react-simple-code-editor:轻量级可定制编辑器
  • monaco-editor:VS Code同款编辑器
  • codemirror:功能丰富的代码编辑器

安装示例(以react-simple-code-editor为例):

react实现行号

npm install react-simple-code-editor prismjs

实现代码:

import Editor from 'react-simple-code-editor';
import { highlight, languages } from 'prismjs';
import 'prismjs/components/prism-javascript';

const CodeEditorWithLineNumbers = () => {
  const [code, setCode] = useState('');

  return (
    <Editor
      value={code}
      onValueChange={code => setCode(code)}
      highlight={code => highlight(code, languages.js, 'javascript')}
      padding={10}
      style={{
        fontFamily: '"Fira code", "Fira Mono", monospace',
        fontSize: 12,
      }}
      textareaId="code-editor"
      preClassName="language-js"
    />
  );
};

样式优化技巧

为获得更好的行号显示效果,可考虑以下CSS方案:

.line-numbers {
  counter-reset: line;
}

.line-numbers div::before {
  counter-increment: line;
  content: counter(line);
  display: inline-block;
  width: 2em;
  padding-right: 1em;
  margin-right: 1em;
  text-align: right;
  color: #999;
  border-right: 1px solid #ddd;
}

动态行号更新

对于需要频繁更新的内容,使用useMemo优化性能:

const DynamicLineNumbers = ({ content }) => {
  const lineCount = useMemo(() => content.split('\n').length, [content]);

  return (
    <div>
      {Array.from({ length: lineCount }).map((_, i) => (
        <div key={i}>{i + 1}</div>
      ))}
    </div>
  );
};

以上方案可根据具体需求选择,从简单文本显示到专业代码编辑器均有覆盖。

标签: 行号react
分享给朋友:

相关文章

react 如何运行

react 如何运行

运行 React 项目的步骤 安装 Node.js 确保系统已安装 Node.js(建议版本 16+),可通过官网下载并安装。安装后验证版本: node -v npm -v 创建 React 项目…

如何开发react

如何开发react

开发React应用的基本步骤 安装Node.js和npm 确保系统中已安装Node.js(包含npm)。可通过官网下载安装包,安装后验证版本: node -v npm -v 创建React项目…

如何用react

如何用react

使用React的基本步骤 安装Node.js和npm 确保系统已安装Node.js和npm。Node.js自带npm,可从官网下载安装包。安装完成后,通过命令行验证版本: node -v npm -…

react如何打包

react如何打包

打包 React 项目的基本步骤 React 项目通常使用 create-react-app 或类似的脚手架工具创建,这些工具内置了打包功能。以下是打包 React 项目的详细方法: 安装依赖并构建…

react 如何debug

react 如何debug

React 调试方法 使用 React Developer Tools React Developer Tools 是浏览器扩展,支持 Chrome 和 Firefox。安装后,可以在开发者工具中查看…

react native如何

react native如何

React Native 基础知识 React Native 是一个由 Facebook 开发的框架,用于使用 JavaScript 和 React 构建跨平台移动应用。它允许开发者编写一次代码,同时…