A
A
Alexey Milyutin2019-07-05 23:48:15
JavaScript
Alexey Milyutin, 2019-07-05 23:48:15

Why doesn't object change work?

Good afternoon, I'm writing that doo list on hooks. I have a problem, why the handleClose method does not work. I will be very grateful for the answer.

function App() { 
  const [todo, setTodos] = useState(
    [
      { text: 'example', done: false, checked: false },
      { text: 'second example', done: false, checked: false }
    ]
  );
const handleClose = (index) => {
    setTodos((state) => {
      return state[index].checked = true;
    });
  };
return (
    <div className='root-wrapper'>
      <div className='top-wrapper'>
        <h1>Todo List Hooks</h1>
        <MakeDo 
          handleAdd={handleAdd}
        />
      </div>
      <div className='main-wrapper'>
        <List 
          handleClose={handleClose}
          handleDelete={handleDelete}
          items={todo}
          // removeItems={setTodos([])}
        />
      </div>
      <div className='bot-wrapper'>
        <BottomInputs 
          handleClear={handleClear}
          handleAccept={handleAccept}
        />
      </div>
      <input 
        type='button'
        onClick={handleJSON}
        value='addJSON'
      />
    </div>
  )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-07-06
@doppelgangerz

const handleClose = useCallback(index => {
  setTodos(state => {
    const newState = [...state],
    newState[index] = { ...newState[index], checked: true },

    return newState;
  });
}, []);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question