Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question