Welcome back to CodeYourCraft! Today, we're diving into the world of form inputs in React JS. Form inputs are essential for gathering user data, and in this lesson, we'll cover the input, textarea, and select elements.
Form inputs allow users to interact with your application, providing valuable data that can be processed and used to create dynamic and engaging user experiences. By mastering form inputs, you'll be able to build more interactive and functional web applications.
The input element is the most basic form input type. It's used for single-line text input, such as names, email addresses, and passwords.
Let's create a simple input field:
import React from 'react';
function App() {
const [name, setName] = React.useState('');
const handleInputChange = (event) => {
setName(event.target.value);
}
return (
<div>
<input type="text" value={name} onChange={handleInputChange} />
<p>Your name is: {name}</p>
</div>
);
}
export default App;š Note: In the code above, we use the useState hook to manage the name state, and an event handler handleInputChange to update the name whenever the input field's value changes.
Here are some common input types:
text: Single-line text input (default value)email: Email input with email validationpassword: Password input with asterisks instead of plain textnumber: Numeric inputsearch: Search input with magnifying glass iconThe textarea element is used for multi-line text input, such as comments or descriptions.
Let's create a simple textarea:
import React, { useState } from 'react';
function App() {
const [comment, setComment] = React.useState('');
const handleTextareaChange = (event) => {
setComment(event.target.value);
}
return (
<div>
<textarea value={comment} onChange={handleTextareaChange}></textarea>
<p>Your comment is: {comment}</p>
</div>
);
}
export default App;š Note: The textarea element behaves similarly to the input element, but it accepts multiple lines of text.
The select element is used for dropdown menus, allowing users to select an option from a predefined list.
Let's create a simple select menu:
import React, { useState } from 'react';
function App() {
const [selectedOption, setSelectedOption] = React.useState('');
const handleSelectChange = (event) => {
setSelectedOption(event.target.value);
}
const options = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'option3', label: 'Option 3' },
];
return (
<div>
<select value={selectedOption} onChange={handleSelectChange}>
{options.map((option, index) => (
<option key={index} value={option.value}>{option.label}</option>
))}
</select>
<p>Your selected option is: {selectedOption}</p>
</div>
);
}
export default App;š Note: In the code above, we use an array of objects to store the option values and labels, and map over the array to create the option elements for the select menu.
What does the `input` element in React JS primarily serve for?
That's it for today! In the next lesson, we'll dive deeper into React JS form inputs and explore more advanced concepts, such as handling form submissions and form validation. Stay tuned and keep coding! š¤ Happy learning! š