React Textarea Autosize

8.5.9 · active · verified Sun Apr 19

react-textarea-autosize is a React component that serves as a high-performance, lightweight (approx. 1.3KB minified & gzipped) drop-in replacement for the native `<textarea>` HTML element, automatically adjusting its height to fit its content. Inspired by the popular jQuery Autosize plugin, it provides a seamless user experience for input fields that need to expand dynamically. The package is currently at version 8.5.9 and maintains an active release cadence with frequent patch updates to address bugs and improve compatibility. Key differentiators include its small size, broad React compatibility (supporting React 16.8.0 through 19.0.0), and specific features like `minRows`, `maxRows`, and an `onHeightChange` callback. Recent enhancements have focused on improving module resolution for modern JavaScript runtimes such as Vercel Edge and Cloudflare Workers, resolving race conditions, and bolstering Node.js ESM support.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to integrate `TextareaAutosize` into a React application, showing basic usage with state management, `minRows`, `maxRows`, and the `onHeightChange` callback.

import React from 'react';
import { createRoot } from 'react-dom/client';
import TextareaAutosize from 'react-textarea-autosize';

const App = () => {
  const [value, setValue] = React.useState('Hello, world!\nThis is an autosizing textarea.\nTry typing more to see it grow.');

  return (
    <div style={{ padding: '20px', maxWidth: '400px', margin: 'auto' }}>
      <h1>Autosizing Textarea Demo</h1>
      <TextareaAutosize
        value={value}
        onChange={(e) => setValue(e.target.value)}
        minRows={3}
        maxRows={10}
        placeholder="Type something here..."
        style={{
          width: '100%',
          padding: '8px',
          fontSize: '16px',
          lineHeight: '1.5',
          border: '1px solid #ccc',
          borderRadius: '4px',
          boxSizing: 'border-box', // Crucial for correct sizing
          resize: 'none', // Disable native resize
        }}
        onHeightChange={(height, { rowHeight }) => {
          console.log(`Textarea height changed to: ${height}px (rowHeight: ${rowHeight}px)`);
        }}
      />
      <p style={{ marginTop: '20px' }}>Current value: {value}</p>
    </div>
  );
};

const container = document.getElementById('root');
if (container) {
  const root = createRoot(container);
  root.render(<App />);
} else {
  console.error("Root element not found.");
}

view raw JSON →