React Js Add Element or Items to Array by using Input Field
If you want to learn how to manipulate arrays in React, this article is for you. You will discover how to use various methods and hooks to add, remove, update, and render elements in an array. You will also learn how to store input values in an array and create components for each item in an array. Whether you need to use the spread syntax, the useState hook, the map function, or the concat method, this article will show you how to do it with clear examples and explanations.




Thanks for your feedback!
Your contributions will help us to improve service.
How to Store Input Value in Array in React Js?
React Js Add Element or Items to Array by using Input Field: In React.js, you can add elements to an array using input elements by first creating a state variable for the array using the useState hook. Then, you can create an input element with an onChange event that updates the state variable whenever the user types in a new value. To add the new value to the array, you can use the spread operator to create a new array with the old values plus the new value.
React Js Add Element or Items to Array by using Input Field
xxxxxxxxxx
<script type="text/babel">
function App() {
const { useState } = React;
const [items, setItems] = useState(['Apple', 'Mango']); // State to hold the array of items
const [inputValue, setInputValue] = useState(''); // State to hold the input field value
const handleInputChange = (event) => {
setInputValue(event.target.value);
};
const handleAddItem = () => {
if (inputValue.trim() !== '') {
setItems((prevItems) => [...prevItems, inputValue]);
setInputValue(''); // Clear the input field after adding the item
}
};
return (
<div className='container'>
<h3>React Js Add Element or Items to Array bu Input field</h3>
<div className='form'>
<input type="text" value={inputValue} onChange={handleInputChange} />
<button onClick={handleAddItem}>Add Item</button>
</div>
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js Add Element or Items to Array by using Input Field
Add element to array javascript
In this example, you will learn how to add elements to an array in JavaScript. You will also see some examples and tips on how to use them effectively. Whether you want to append elements to the end or the beginning of an array, insert elements at a specific index, or join two arrays together, this article has you covered.
Javascript Add Item to Array Example
xxxxxxxxxx
<script>
// Initialize an empty array
let myArray = [];
function addToMyArray() {
// Get the input element value
let inputValue = document.getElementById('myInput').value;
// Check if the input is not empty
if (inputValue.trim() !== '') {
// Add the value to the array
myArray.push(inputValue);
// Update the output div with array content
updateOutput();
} else {
alert("Please enter a value before adding to the array.");
}
}
function updateOutput() {
// Get the output div element
let outputDiv = document.getElementById('output');
// Clear previous content
outputDiv.innerHTML = '';
// Display array content
for (let i = 0; i < myArray.length; i++) {
outputDiv.innerHTML += `<p>${myArray[i]}</p>`;
}
}
</script>