# Lists in React
Let us suppose we have a list of products,
`productArray = [laptop, headphones, mouse]`
1. Let us create a component named `Products`, so we will create a file named `Products.js` inside the components folder.
2. Now open `Products.js`, then type `rfce` and press enter.
```javascript
import React from 'react'
function Products(){
return{
Products
}
}
export default Products
```
3. Let us assume we have a `products` array, which contains products.
```javascript
import React from 'react'
function Products(){
let products = ['Laptop', 'Headphones', 'Keyboard', 'Mouse']
return{
Products
}
}
export default Products
```
4. Now we want to show all the elements of the `products` array in the HTML.
5. The first way is to simply iterate and display every element.
```javascript
import React from 'react'
function Products(){
let products = ['Laptop', 'Headphones', 'Keyboard', 'Mouse']
return{
for(let i = 0;i < products.length; i++){
}
}
}
export default Products
```
Here syntax is correct, but it will give an error, as JSX does not allow us to write a loop in this way. In place of loops, we can higher-order functions.
6. We will use `map` with an array, by which we get access to all the array elements one by one. We will create a list element of every element one by one.
```javascript
import React from 'react'
function Products(){
let products = ['Laptop', 'Headphones', 'Keyboard', 'Mouse']
return{
{products.map((product) => {
return
{product}
})}
}
}
export default Products
```
7. Now we need to import the `Products` component in `App`.
```javascript=
import Products from './components/Products';
function App(){
return {
};
}
export default App
```
When we run this, and we will see the output we will get the correct output, but a warning is also there in the console.

This warning says that every child in a list should have a unique 'key' prop.
- React says whenever you create a list element, you should provide a unique key to it so that when React updates the DOM, it can uniquely identify what is required to be updated.
The key is a unique ID.
So we will create a key for every list element, and we are creating the key with the product name, as every product has a different name.
```javascript
import React from 'react'
function Products(){
let products = ['Laptop', 'Headphones', 'Keyboard', 'Mouse']
return{
{products.map((product) => {
return
{product}
})}
}
}
export default Products
```
- But in case, we have two products with the same name, `let products = ['Laptop', 'Headphones', 'Keyboard', 'Mouse', 'Laptop']`
```javascript
import React from 'react'
function Products(){
let products = ['Laptop', 'Headphones', 'Keyboard', 'Mouse', 'Laptop']
return{
{products.map((product) => {
return
{product}
})}
}
}
export default Products
```
- Now the problem will occur, as the two products have the same name, so the key will not remain unique as the same laptop key is assigned to two products.
- Now we will again get a warning `encountered two children with a same key laptop`.
- So we can use index, `map()` will also have access to all the indexes. And every array element has a unique index, starting from 0, 1, 2, 3, .....
- So we can make the key using the index.
```javascript
import React from 'react'
function Products(){
let products = ['Laptop', 'Headphones', 'Keyboard', 'Mouse', 'Laptop']
return{
{products.map((product, index) => {
return
{product}
})}
}
}
export default Products
```
## Using objects
1. We will create an array of objects for products.
```javascript=
import React from 'react'
function Products(){
let productsList = [
{ id: 1, name: "Laptop", price: 35000},
{ id: 2, name: "Headphones", price: 12000},
{ id: 3, name: "Mouse", price: 8000},
{ id: 4, name: "Keyboard", price: 10000},
]
return{
}
}
export default Products
```
2. Now we will display this product list in HTML using `map`. Now we are using the `id` of every object of an array for creating the key and then we are displaying the name and price of that particular product.
```javascript=
import React from 'react'
function Products(){
let productsList = [
{ id: 1, name: "Laptop", price: 35000},
{ id: 2, name: "Headphones", price: 12000},
{ id: 3, name: "Mouse", price: 8000},
{ id: 4, name: "Keyboard", price: 10000},
]
return{
{
productsList.map((product) => {
return
{product.name} : {product.price}
})
}
}
}
export default Products
```
**Output:**

- We can also remove the return keyword written before ``, but then we need to enclose the list element creation code in `()`, instead of `{}`.
```javascript=
import React from 'react'
function Products(){
let productsList = [
{ id: 1, name: "Laptop", price: 35000},
{ id: 2, name: "Headphones", price: 12000},
{ id: 3, name: "Mouse", price: 8000},
{ id: 4, name: "Keyboard", price: 10000},
]
return{
{
productsList.map((product) => (
{product.name} : {product.price}
))
}
}
}
export default Products
```
# Form Handling in React
- **Form** is a layout which will accept data from the user.
- Form has text boxes, labels, checkboxes, buttons, etc. by which the user can provide some input.
- Then the user will click on submit, then all the data will go to the database, or be sent for some processing.
**In react, we can also work with forms**.
1. Let us create a component named `Form`, so first we will create a file named `Form.js` inside the components folder.
2. Now open `Form.js`, then type `rfce` and press enter.
```javascript
import React from 'react'
function Form(){
return{
Form
}
}
export default Form
```
3. Let us create a form by the `