如何在 ReactJS 中使用 TextareaAutosize 组件?

原文:https://www . geeksforgeeks . org/如何使用-textareautosize-component-in-reactjs/

用于反应的文本区组件,随内容增长。【React 的 Material UI 有这个组件可供我们使用,非常容易集成。我们可以使用下面的方法在 ReactJS 中使用 TextareaAutosize 组件。

创建反应应用程序并安装模块:

步骤 1: 使用以下命令创建一个 React 应用程序。

npx create-react-app foldername

步骤 2: 在创建项目文件夹(即文件夹名)后,使用以下命令移动到该文件夹。

cd foldername

步骤 3: 创建 ReactJS 应用程序后,使用以下命令安装 material-ui 模块。

npm install @material-ui/core

项目结构:如下图。

项目结构

示例:现在在 App.js 文件中写下以下代码。在这里,App 是我们编写代码的默认组件。

App.js

import React from 'react';
import TextareaAutosize from '@material-ui/core/TextareaAutosize';

export default function App() {

  return (
    <div style={{ display: 'block', padding: 30, width: 500 }}>
      <h4>How to use TextareaAutosize Component in ReactJS?</h4>
      <TextareaAutosize style={{width: 150}} 
      placeholder="Enter your text here!" />
    </div>
  );
}

运行应用程序的步骤:从项目的根目录使用以下命令运行应用程序。

npm start

输出:现在打开浏览器,转到http://localhost:3000/,会看到如下输出。

参考:T2】https://material-ui.com/components/textarea-autosize/