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

167
Visualizações
How can I create states for checkbox Booleans used in a todo list that can have items added/deleted by the user?

I have created a todo list for my site where the user is able to add and delete todo items and I am trying to make it so they can check or uncheck the items as they need to.

Right now the ability to add/delete is working fine and the checkboxes update the database when they are clicked as they are suppose to but the problem is that the checkboxes don't update properly on the website.

The state is an array of todo item information from the database. So initially the state is set as the current information from the database:

const [items, setItems] = useState(todo_items)

where todo_items is from the DB and is structured like this:

```
0: 
 complete: 0 or a 1
 todo_id: Integer
 todo_item: String
1: 
 complete: 0 or a 1
 todo_id: Integer
 todo_item: String
.
.
.
```

Now once this is set as the state I map the todo items to a form:

<form>
     {items.map((e) => (
        <div id={e.todo_id}>
            <div>
               <input
                  type="checkbox"
                  checked={e.complete === 1}
                  onChange={(f) => checkHandler(f.target.checked, e.todo_id)}
               />
               <p>{e.todo_item}</p>
            </div>
         </div>
      ))}
</form>

and then when a checkbox is checked I change the state and update the database to reflect the new representation of all of the checkboxes

async function checkHandler(checked, todo_id){
    var updated_todo = items

    for (var i = 0 ; i < items.length ; i++){
        if (items[i].todo_id === todo_id && checked === true) {
            updated_todo[i].complete = 1;
        } else if (items[i].todo_id === todo_id && checked === false){
            updated_todo[i].complete = 0;
        }
    }
        
    setItems(updated_todo)
    
    // update database below...
}

I thought this would automatically update the checkboxes on the website every time this state is updated but it doesn't re-map the todo items in the form when the items state is changed.

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

0

var updated_todo = items

is a reference to the object so when you change it you also change the state, thats why the app wont rerender when you set the state

try changing that too

var updated_todo = [...items]
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