Course Content
Run Your First Project
This course is designed to help you kickstart your journey in project management. Whether you're a beginner or looking to brush up on your skills, this course will provide you with the essential knowledge and tools needed to successfully manage your first project from start to finish. Join us in "Run Your First Project" and gain the confidence and skills to manage your next project with success!
0/2
Rendering Elements
In React, elements are the smallest building blocks of React applications. React elements are immutable and describe what you want to see on the screen. They are used to construct the user interface by defining how it should look at a given point in time. When rendering, React elements are passed to the React DOM, which efficiently updates and renders the elements to the actual DOM.
0/12
Build Your Application
0/1
Learning Journey Of React
About Lesson

In React, the concepts of “map” and “key” are essential for efficiently rendering lists of elements. Here’s a brief introduction to each:

Map Function in React

The map function is a JavaScript array method that creates a new array populated with the results of calling a provided function on every element in the calling array. In React, it’s commonly used to transform an array of data into an array of JSX elements.

Example

import React, { StrictMode } from "react";
import { createRoot } from "react-dom/client";

const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
  <li>{number}</li>
);

const root = createRoot(document.getElementById("root"));
root.render(
  <StrictMode>
   <ul>{listItems}</ul>
  </StrictMode>
);

Result:

In this example, numbers.map() creates a new array of <li> elements, each containing a number from the numbers array.

Keys in React

A “key” is a special string attribute you need to include when creating lists of elements in React. Keys help React identify which items have changed, been added, or removed. This improves the efficiency of updates and helps maintain the identity of each component.

import React, { StrictMode } from "react";
import { createRoot } from "react-dom/client";

const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
  <li key={number.toString()}>{number}</li>
);

const root = createRoot(document.getElementById("root"));
root.render(
  <StrictMode>
   <ul>{listItems}</ul>
  </StrictMode>
);

In this example, each <li> element is given a key attribute set to the corresponding number’s string representation. This key should be a unique identifier for each element in the array.

.