Skip to content

Unused variable in the code of the tutorial #6405

Closed
@witch-factory

Description

@witch-factory

Hi. I am reading the "Learn React" Page, Updating Arrays in State.

In the example code of the tutorial, there is nextId variable for the next ID for the new item for the array state. But after the Making other changes to an array section, this variable is never used but exists.

import { useState } from 'react';

let nextId = 3;
const initialList = [
  { id: 0, title: 'Big Bellies' },
  { id: 1, title: 'Lunar Landscape' },
  { id: 2, title: 'Terracotta Army' },
];

export default function List() {
  const [list, setList] = useState(initialList);

  function handleClick() {
    const nextList = [...list];
    nextList.reverse();
    setList(nextList);
  }

  return (
    <>
      <button onClick={handleClick}>
        Reverse
      </button>
      <ul>
        {list.map(artwork => (
          <li key={artwork.id}>{artwork.title}</li>
        ))}
      </ul>
    </>
  );
}

Is it intended for something? If not, please remove the declaration of unused variable. Otherwise, it would be very thanksful to me if someone tell me the point of it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions