Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 23db798577 | |||
| c99a045400 |
+35
-30
@@ -1,45 +1,50 @@
|
|||||||
import React, { useMemo, useCallback, useState } from 'react'
|
import { useState, useReducer } from 'react'
|
||||||
|
|
||||||
const TodoItem = React.memo(({ item, onToggle, onDelete }) => {
|
function counterReducer(state, action) {
|
||||||
console.log(`rendred ${item.id}`);
|
switch (action.type) {
|
||||||
return <li>
|
case 'increment':
|
||||||
{item.name}
|
return state + 1
|
||||||
<button onClick={() => onToggle(item.id)}>Done</button>
|
case 'decrement':
|
||||||
<button onClick={() => onDelete(item.id)}>remove</button>
|
return state - 1
|
||||||
</li>
|
case 'reset':
|
||||||
})
|
return 0
|
||||||
|
case 'set_value':
|
||||||
|
return action.value
|
||||||
|
default:
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function ItemList({ items }) {
|
function Counter() {
|
||||||
|
const [count, dispatch] = useReducer(counterReducer,0);
|
||||||
const handleToggle = useCallback(id => {
|
|
||||||
items.done = true;
|
|
||||||
console.log(`Item ${id} done`)
|
|
||||||
console.log(id)
|
|
||||||
},[]);
|
|
||||||
|
|
||||||
const handleDelete = useCallback(id => {
|
|
||||||
items.pop(id);
|
|
||||||
},[]);
|
|
||||||
return (
|
return (
|
||||||
<>
|
<div>
|
||||||
<h3>List items</h3>
|
<span>Счетчик: {count}</span>
|
||||||
{items.map(item => (
|
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
||||||
<TodoItem key={item.id} item={item}
|
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
||||||
onToggle={handleToggle} onDelete={handleDelete} />
|
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
||||||
))}
|
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
||||||
</>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [tasks, setTasks] = useState([])
|
||||||
|
const [filter, setFilter] = useState('all')
|
||||||
|
const [sortBy,setSortBy] = useState('date')
|
||||||
|
|
||||||
|
const addTask = newTask => {
|
||||||
|
setTasks([...tasks,newTask])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<ItemList items={[
|
<Counter />
|
||||||
{id:1,name:'Первый',done:false},
|
|
||||||
{id:2,name:'Второй',done:false}
|
|
||||||
]} />
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
.container {
|
||||||
|
/* display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
flex-wrap: nowrap;*/
|
||||||
|
height: 500px;
|
||||||
|
overflow: auto;
|
||||||
|
border: 1px solid black;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
font-size: 16pt;
|
||||||
|
border: 1px dotted gray;
|
||||||
|
/*margin: 5px 10px;
|
||||||
|
padding: 10px;*/
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
@@ -4,5 +4,7 @@ import { createRoot } from 'react-dom/client'
|
|||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
|
<StrictMode>
|
||||||
<App />
|
<App />
|
||||||
|
</StrictMode>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user