# Element not updating setState

**URL:** https://forum.codewithmosh.com/t/element-not-updating-setstate/17852
**Category:** React
**Created:** [January 22, 2023, 3:26pm UTC](https://forum.codewithmosh.com/t/element-not-updating-setstate/17852 "2023-01-22T15:26:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Dalare](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@Dalare](https://forum.codewithmosh.com/u/Dalare)
#### Post date: [January 22, 2023, 3:26pm UTC](https://forum.codewithmosh.com/t/element-not-updating-setstate/17852/1 "2023-01-22T15:26:19Z")

</div>

Hi, I’m working on a small simple project and the whole idea is to be able to add and delete todos. But the problem I’m having is when I enter the data into a **input** field the event handler adds a new **todo** when I console log it but the UI is not updated in the browser? So in the **input** event handler( **handleSubmit** ) when I reset the **input** value after changing the state of todo then it updates the UI Element in the browser. Same goes with the delete button but this time resetting the **input** doesn’t delete the todo but instead typing any keyword on a **input** filed deletes the UI that was clicked to delete. Not sure what I’m missing here? Any help would be very appreciated. Thank you.

Code:

This is the todo.jsx file

```auto
import React, { useState } from "react";
import { useEffect } from "react";
import { addTodo, getTodos, todoDelete } from "../fakeTodosService";

export default function Todo() {
  const [input, setInput] = useState("");
  const [todos, setTodos] = useState([]);
  const [error, setError] = useState("");

  useEffect(() => {
    setTodos(getTodos());
  }, []);

  function handleDelete(list) {
    setTodos(todoDelete(list));
  }

  function handleSubmit(e) {
    e.preventDefault();

    if (input.trim() === "") setError("Enter value first!");
    else {
      setError("");
      setTodos(addTodo(input));
      setInput(""); //when this is removed the newly added todo won't show in the browser
    }
  }

  function handleChange({ currentTarget: input }) {
    const inputValue = input.value;
    setInput(inputValue);
  }

  return (
    <div className="vh-100 vw-100" style={{ backgroundColor: "#F8F8F8" }}>
      <div className="pt-5">
        <h2>To-Do Lists</h2>
      </div>
      <div className="w-100 h-100 p-5">
        <form onSubmit={handleSubmit}>
          <div className="row w-100">
            <div className="col">
              <input
                className="form-control"
                type="text"
                placeholder="Enter To-do's"
                name="todo"
                id="todo"
                onChange={handleChange}
              />
              {error && <div className="alert alert-danger">{error}</div>}
            </div>
            <button className="btn btn-primary col-1" onClick={handleSubmit}>
              Add
            </button>
          </div>
        </form>
        {todos.length === 0 ? (
          <h3>There are no To-do's in the Database</h3>
        ) : (
          <table className="table table-striped mt-5">
            <tbody>
              {todos.map((list) => (
                <tr key={list._id}>
                  <td>{list.body}</td>
                  <td>
                    <button
                      className="btn btn-danger"
                      onClick={() => handleDelete(list)}
                    >
                      Delete
                    </button>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        )}
      </div>
    </div>
  );
}

```

this is the fakeTodoService file "

```auto
const todos = [
  {
    _id: "1",
    body: "Wash Clothes",
  },
  {
    _id: "2",
    body: "Fold Clothes",
  },
  {
    _id: "3",
    body: "Clean Apartment",
  },
  {
    _id: "4",
    body: "Learn React",
  },
  {
    _id: "5",
    body: "Learn Node.js",
  },
  {
    _id: "6",
    body: "Start Project",
  },
];

export function getTodos() {
  return todos;
}

export function addTodo(input) {
  const todo = {};
  todo._id = Date.now() + Math.floor(Math.random() * 99);
  todo.body = input;
  todos.push(todo);
  return todos;
}

export function todoDelete(list) {
  const index = todos.indexOf(list);
  todos.splice(index, 1);
  return todos;
}

```

---

<div class="post-metadata">

### Author: ![sufian.babri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sufian.babri/32/1028_2.png) [@sufian.babri](https://forum.codewithmosh.com/u/sufian.babri)
#### Post date: [January 29, 2023, 10:10am UTC](https://forum.codewithmosh.com/t/element-not-updating-setstate/17852/2 "2023-01-29T10:10:50Z")

</div>

You should declare `id` at the top if the file like so:

```auto
let lastId = 0;

```

And then when creating the Todo item, you can use it and increment it.

As for the bug, I think you should make some changes to the addTodo function.

Instead of writing `todos.push(todo)`, you can try writing `return [...todos, todo] `.

---

<div class="post-metadata">

### Author: ![Harvel](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/harvel/32/7651_2.png) [@Harvel](https://forum.codewithmosh.com/u/Harvel)
#### Post date: [January 31, 2023, 3:25pm UTC](https://forum.codewithmosh.com/t/element-not-updating-setstate/17852/3 "2023-01-31T15:25:28Z")

</div>

Hey its Harvel, do you want to change the UI as you change the input value or only on submission? I’ve got a solution for both
