Reusable Textarea fields in React are custom components that encapsulate textarea elements and their behavior, allowing you to easily incorporate consistent and customizable form inputs throughout your application, enhancing maintainability and code reusability.
Textarea Component
This code defines a reusable React component called TextareaField, which can be used to render a labeled textarea input field. The label, current value, and an onChange callback function are passed as props to customize the behavior and appearance of the component when used in different parts of a React application
File Name – TextareaField.js
import React from 'react';
const TextareaField = ({ label, value, onChange }) => {
return (
<div>
<label>{label}</label>
<textarea value={value} onChange={onChange}></textarea>
</div>
);
};
export default TextareaField;
- The code imports the React library to create React components.
- Define the
TextareaFieldcomponent: This is a functional React component that takes three props:label,value, andonChange. - The component’s JSX code renders a
divcontaining alabelelement for displaying the label text and atextareaelement for user input. valueandonChangeProps: Thetextareaelement’svalueandonChangeprops are set to the values passed in as props to theTextareaFieldcomponent. This allows for controlled input, where thevaluereflects the current content of the text and theonChangefunction is called when the textarea content changes.- The
TextareaFieldcomponent is exported as the default export of the module so it can be used in other parts of a React application.
Use Reusable Textare Field Component
You have created a Textarea field component, you can reuse it multiple times by passing its required props – label, value & onChange.
This code defines a React functional component (App) that renders a simple form-like UI with two text areas for entering a “Description” and a “Short Description.” It uses the useState hook to manage the state of these text areas and updates the state as the user types. This code assumes that the TextareaField component is defined elsewhere in your codebase and is used to encapsulate the textarea input fields.
File Name – App.js
import React, { useState } from 'react';
import TextareaField from './TextareaField';
const App = () => {
const [description, setDescription] = useState('');
const [shortDescription, setShortDescription] = useState('');
return (
<div>
<h1>Reusable Form Components Example</h1>
{/* Description */}
<TextareaField
label="Description"
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
{/* Short Description */}
<TextareaField
label="Short Description"
value={shortDescription}
onChange={(e) => setShortDescription(e.target.value)}
/>
</div>
);
};
export default App;
- Imports React and the
useStatehook, which allows for managing component state. - Imports a custom
TextareaFieldcomponent from an external file. - Defines the
Appfunctional component. - Uses the
useStatehook to create state variables fordescriptionandshortDescription. - Returns JSX code representing the user interface, including a title and two
TextareaFieldcomponents for “Description” and “Short Description.” These components receive props for their labels, values, and onChange handlers. - Exports the
Appcomponent for use in other parts of the application.