This document explains how to declare, traverse, and manage arrays in React components, including state management, rendering lists, and common array methods.
This document covers how to declare, traverse, and manage arrays in React components, including state management, rendering lists, and using array methods like map, forEach, and splice for dynamic UIs.
Arrays are fundamental data structures in JavaScript, used to store multiple values in a single variable. In React, arrays are essential for managing lists of data and building dynamic user interfaces.
Arrays can be declared using array literal notation:
1const items = ['autumn', 'spring', 'summer', 'winter']
Arrays can also be stored in component state using the useState hook:
1import React, { useState } from 'react'
2
3function Seasons() {
4 const [items, setItems] = useState(['autumn', 'spring', 'summer', 'winter'])
5 // ...
6}
Arrays may be constructed dynamically based on logic or received data.
React components often need to traverse arrays to render lists or manage data. Common methods include:
1function SeasonList() {
2 const items = ['autumn', 'spring', 'summer', 'winter']
3 return (
4 <div>
5 <h1>Season Names</h1>
6 <ul>
7 {items.map((item, index) => (
8 <li key={index}>{item}</li>
9 ))}
10 </ul>
11 </div>
12 )
13}
You can add or remove items from an array using state and setState:
1import React, { useState } from 'react'
2
3function SeasonsManager() {
4 const [items, setItems] = useState(['autumn', 'spring', 'summer', 'winter'])
5 const [inputValue, setInputValue] = useState('')
6
7 function addItem() {
8 setItems([...items, inputValue])
9 setInputValue('')
10 }
11
12 function removeItem(index) {
13 const newItems = [...items]
14 newItems.splice(index, 1)
15 setItems(newItems)
16 }
17
18 return (
19 <div>
20 <h1>Seasons</h1>
21 <ul>
22 {items.map((item, index) => (
23 <li key={index}>
24 {item} <button onClick={() => removeItem(index)}>Remove</button>
25 </li>
26 ))}
27 </ul>
28 <input
29 value={inputValue}
30 onChange={e => setInputValue(e.target.value)}
31 placeholder="Add a season"
32 />
33 <button onClick={addItem}>Add</button>
34 </div>
35 )
36}
You can conditionally render components based on array content:
1import React, { useState } from 'react'
2
3function LanguageList() {
4 const [items, setItems] = useState(['HTML', 'CSS', 'JavaScript'])
5 return (
6 <div>
7 <h1>Front-end languages</h1>
8 {items.length > 0 ? (
9 <ul>
10 {items.map((item, index) => (
11 <li key={index}>{item}</li>
12 ))}
13 </ul>
14 ) : (
15 <p>No Front-end language is available</p>
16 )}
17 </div>
18 )
19}
Arrays are powerful data structures for storing and managing groups of items in React components. Using methods like map, forEach, push, and splice, you can efficiently render, update, and manipulate lists for dynamic UIs.
(2) Arrays in React components are used to manage lists of data and are essential for building dynamic and interactive user interfaces.
(3) Omitting the key attribute can cause React to inefficiently update the list, potentially resulting in rendering issues or unexpected behavior.
(3) The map method does not modify the original array; it returns a new array based on the callback function.
(1) The useState hook enables arrays to be stored and updated as part of a component’s state, supporting dynamic data management.
(3) The addItem function should be checked to ensure it updates the state correctly when adding new items to the array.
| Method | Description |
|---|---|
| A. map | 1. Adds or removes elements at a specific index |
| B. forEach | 2. Runs a function on each element without creating a new array |
| C. push | 3. Adds elements to the end of an array |
| D. splice | 4. Creates a new array by applying a function to each element |
A-4, B-2, C-3, D-1.
The forEach method in JavaScript returns a new array containing the results of the callback function.
False. The forEach method executes a function for each array element but does not return a new array.
(2) Conditional rendering does not always require a ternary operator; other approaches like logical && can also be used.
(2) The key attribute helps React efficiently identify which items have changed, are added, or are removed during re-rendering.
(2) To remove an item, create a copy of the array, remove the item, and update the state with the new array to ensure React re-renders correctly.