Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

334
Visualizações
Delete last item from array with useState

I'm new to JS, React and TypeScript. I did a tutorial to add a todo-list. To get some practice, I decided to add the delete button and the "remove last item" button.

Deleting the complete list worked out fine (I'm proud of myself, hah!) but the "delete last item" is not working, I tried different things (e.g. just todos.pop()).


function App() {
  const [todos, setTodos] = useState([])
  const [input, setInput] = useState("")

  // prevents default, adds input to "todos" array and removes the input from form
  const addTodo = (e) => {
    e.preventDefault()

    setTodos([...todos, input])
    setInput("")
  }

  // deletes the todo-list
  const clearTodo = (e) => {
    e.preventDefault()

    setTodos([])
  }

  // deletes the last entry from the todo-list
  const clearLastTodo = (e) => {
    e.preventDefault()

    setTodos(todos.pop())
  }

  return (
    <div className="App">
      <h1>ToDo Liste</h1>
      <form>
        <input 
          value={input} 
          onChange={(e) => setInput(e.target.value)} 
          type="text" 
        />
        <button type="submit" onClick={addTodo}>
          Hinzufügen
        </button>
      </form>

      <div>
        <h2>Bisherige ToDo Liste:</h2>
        <ul>
        {todos.map((todo) => (
          <li>{todo}</li>
        ))}
        </ul>
      </div>

      <div>
        <form action="submit">
        <button type="submit" onClick={clearLastTodo}>
            Letzten Eintrag löschen
          </button>
          <button type="submit" onClick={clearTodo}>
            Liste löschen
          </button>
        </form>
      </div>
    </div>
  );
}

export default App;

Am I missing something (clearly I am, otherwise it would work)? But what? :D

Thank you in advance!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There are 3 possible solutions to your problem.


Solution 1: Slice the array from the first element to the -1 (1 before the last) element.

setTodos(todos.slice(0, -1)));

Or

setTodos((previousArr) => (previousArr.slice(0, -1)));

Solution 2: Create a copy array and splice it with the value of -1. Then set the array from the first element to the -1 element.

const copyArr = [...todos];
copyArr.splice(-1);
setTodos(copyArr);

Solution 3: Create a copy list with the ..., pop the copy and set the new value of the array to the copy.

const copyArr = [...todos];
copyArr.pop();
setTodos(copyArr);
about 4 years ago · Juan Pablo Isaza Relatório

0

You could do it as below. The spread operator makes sure you don't give the same reference to setTodos:

const clearLastTodo = (e) => {
    e.preventDefault();
    let copy = [...todos]; // makes sure you don't give the same reference to setTodos
    copy.pop()
    setTodos(copy); 
  }

A note about state in React:

Always treat state variables as if they were immutable. It's obvious for for primitive values like Number, Boolean... But for Objects and Arrays changing their content doesn't make them different, it should a new memory reference.

about 4 years ago · Juan Pablo Isaza Relatório

0

There are couple way to remove the last item from an array in javascript

const arr = [1, 2, 3, 4, 5]
arr.splice(-1) // -> arr = [1,2,3,4]
// or
arr.pop()      // -> arr = [1,2,3,4]
// or
const [last, ...rest] = arr.reverse()
const removedLast = rest.reverse()

Following the guide updating objects and arrays in state

const onRemoveLastTodo = () => {
    // Try with each way above
  setTodos(todos.splice(-1))
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda