1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| const todos = [ [ { id: 1, value: 'Wash dishes' }, { id: 2, value: 'Clean the bed' }, { id: 3, value: 'Running' }, { id: 4, value: 'Learning' } ], [ { id: 4, value: 'Learning' }, { id: 1, value: 'Wash dishes' }, { id: 2, value: 'Clean the bed' }, { id: 3, value: 'Running' }, ] , [ { id: 2, value: 'Clean the bed' }, { id: 4, value: 'Learning' }, { id: 1, value: 'Wash dishes' }, { id: 3, value: 'Running' }, ] , [ { id: 2, value: 'Clean the bed' }, { id: 3, value: 'Running' }, { id: 4, value: 'Learning' }, { id: 1, value: 'Wash dishes' }, ] ]
function App() { const [items, setItems] = React.useState(todos[0]) React.useEffect(() => { const interval = setInterval(() => { const random = Math.floor(Math.random() * 3) setItems(todos[random]) }, 1000) return () => { clearInterval(interval) } }, []) const handleDoneClick = (todo) => { setItems(items => items.filter(item => item !== todo)) } const handleRestoreClick = () => { setItems(items => [...items, todos.find(item => !items.includes(item))]) } return ( <> {items.map((todo,index) => ( <div key={todo.id}> <button onClick={() => handleDoneClick(todo)}>{todo.value}</button> </div> ))} <br /> <br /> <button onClick={handleRestoreClick}>Restore</button> </> ); }
ReactDOM.render(<App />, root)
|