Çetin
Çetin

Reputation: 127

Cannot handle state of checkbox upon page reload in React

I'm trying to make a todo list app by using React and Material UI. Everything works fine except checkbox state. I'm saving each todo as object in todo list array in local storage as below.

 let arr = todoList;
 const newTodo = new TodoItem({
                id: randomID,
                text: text,
                date: date,
                status: isCompleted
            });

 setTodoList([newTodo, ...arr]);
            localStorage.setItem('todos', JSON.stringify(todoList));

In another component I get saved items from local storage by useEffect and render them as todo list. I'm trying to get stored items every time isCompleted changes.

 useEffect(() => {
        let savedItem = JSON.parse(localStorage.getItem('todos'))
        if (savedItem !== null) {
            setTodoList(savedItem);
        }
    }, [isCompleted])
  <FormControlLabel
       control={
       <Checkbox
       onChange={(e) => changeStatus(e.target.checked,index)}
       color="primary"
        />
       }
  />
 const changeStatus = (val,num) => {

        let item = todoList[num];
        if(val === false){
            setIsCompleted(true);
            item.status = isCompleted;

        }else if(val === true){
            setIsCompleted(false);
            item.status = isCompleted;
        }
        
        let storedItems = [...todoList];
        itemsToRender = storedItems;
        storedItems = storedItems.map(todo => todo.id === item.id ? todo = item : todo)
        console.log('STORED',storedItems);
        localStorage.setItem('todos', JSON.stringify(storedItems));
    }

How can I prevent checkbox to reset itself(unchecked), I need checkbox to stay checked after reloading the page.

To me, I'm doing something wrong with useEffect or MUI checkbox component.

Thanks in advance for any help or advice.

Upvotes: 0

Views: 1304

Answers (1)

Som Shekhar Mukherjee
Som Shekhar Mukherjee

Reputation: 8168

You need to make the checkbox a controlled input by specifying the value attribute on it. And set the value attribute equal to the todo.status

todoList.map(todo => 
    (<FormControlLabel
        control={
            <Checkbox
               onChange={(e) => changeStatus(e.target.checked,index)}
               color="primary"
               value={todo.status}
            />
        }
    />)
)

Upvotes: 1

Related Questions