Skip to content

Latest commit

 

History

History
94 lines (67 loc) · 4.06 KB

08.RenderingUpdatingConditionaling_Lists.md

File metadata and controls

94 lines (67 loc) · 4.06 KB

The Power of Lists: How to Render Them in React with Ease

If you're new to React, you might be wondering how to render lists in your components. Or, if you're an experienced developer, you might be looking for ways to improve your list rendering skills.We'll look at different techniques for rendering lists, including the map function, the for loop, and the useEffect hook. We'll also cover best practices for rendering lists in a React application, and how to optimize your code for performance.

How we can render a list in React ?

In React, you can render a list by using the map method on an array, and returning a JSX element for each item in the array. Here's an example of how you could render a list of numbers:

import React from "react";

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

In this example, the map method iterates over the numbers array and creates a new array of li elements. The resulting array is then rendered as a ul element, with each li element representing an item in the list.

It's important to note that each item in the list should have a unique key prop, which helps React identify which items have changed, been added, or been removed. In the example above, the key prop is set to the string version of the number.

How we achieve the updated new array without losing previous data in it in react by using useState function?

To update the state of an array in React without losing the previous data, you can use the useState hook and the spread operator (...). The useState hook allows you to add state to functional components, and the spread operator allows you to create a new copy of an array, rather than mutating the original array.

Here's an example of how you could use useState and the spread operator to add an item to an array:

import { useState } from "react";

function Example() {
  const [items, setItems] = useState([]);

  const addItem = () => {
    setItems([...items, "new item"]);
  };

  return (
    <div>
      <button onClick={addItem}>Add item</button>
      <ul>
        {items.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
    </div>
  );
}

In this example, the useState hook is used to add state to the Example component, with an initial value of an empty array. The addItem function creates a new copy of the items array using the spread operator, and adds a new item to the end of the array. The updated array is then passed to the setItems function, which updates the state of the items array.

When the addItem function is called, the Example component will re-render, and the new item will be added to the list. This approach ensures that the previous data in the items array is not lost, and the state is updated in an immutable way.

How we display the conditional content in React ?

In React, you can display conditional content by using a combination of JavaScript and JSX. Here's an example of how you could use an if statement to conditionally render content:

import React from "react";

function Example(props) {
  const showContent = props.showContent;

  if (showContent) {
    return <div>This content is visible</div>;
  } else {
    return <div>This content is not visible</div>;
  }
}

In this example, the Example component renders a div element with different content based on the value of the showContent prop. If the showContent prop is true, the component will render the first div element. If the showContent prop is false, the component will render the second div element.

You can also use a ternary operator (?) to concisely render conditional content in a JSX expression:

import React from "react";

function Example(props) {
  const showContent = props.showContent;

  return (
    <div>
      {showContent ? "This content is visible" : "This content is not visible"}
    </div>
  );
}

In this example, the JSX expression will evaluate the showContent prop and render the appropriate content based on its value.