Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 10e56d8aaa |
+72
-56
@@ -1,67 +1,83 @@
|
|||||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
import memo, { useMemo, useCallback, useState } from 'react'
|
||||||
|
|
||||||
function tasksReducer(state, action) {
|
function MyComponent() {
|
||||||
switch(action.type) {
|
const [count, setCount] = useState(0);
|
||||||
case 'ADD_TASK':
|
const [name, setName] = useState('John');
|
||||||
return [...state, action.payload]
|
|
||||||
case 'TOGGLE_TASK':
|
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
||||||
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
|
||||||
case 'REMOVE_TASK':
|
return (
|
||||||
return state.filter(task => task.id !== action.payload)
|
<>
|
||||||
default:
|
<h2>{greeting}</h2>
|
||||||
return state
|
<button onClick={()=>setName('Alex')}>New name</button>
|
||||||
}
|
</>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
function MyComponent2() {
|
||||||
console.log("Rendred")
|
const [data,setData] = useState([{theme:'light'}])
|
||||||
|
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
||||||
|
|
||||||
|
const processData = useMemo(()=>{
|
||||||
|
return data.map(item => ({ ...item,theme: userPreferences.theme }))
|
||||||
|
},[data, userPreferences]);
|
||||||
|
console.log(processData);
|
||||||
return (
|
return (
|
||||||
<ul>
|
<>
|
||||||
{tasks.map(({id, text, complite}) => (
|
<h2 className={data.theme}>2</h2>
|
||||||
<li key={id}
|
<button onClick={()=>setData(data)}>set new theme</button>
|
||||||
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
</>
|
||||||
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
|
||||||
{text}
|
|
||||||
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
|
||||||
Удалить
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
|
||||||
|
function MyComponent3() {
|
||||||
|
const sum = useMemo(()=> a+b,[a,b]);//Бессмысленное применение и вредное
|
||||||
|
|
||||||
|
//Применение оправдано
|
||||||
|
const complexResult = useMemo(()=>{
|
||||||
|
return heavyMethOperations(largeDataSet);
|
||||||
|
},[largeDataSet])
|
||||||
|
}
|
||||||
|
|
||||||
|
function ParentComponent({ items }) {
|
||||||
|
const [filter, setFilter] = useState('');
|
||||||
|
|
||||||
|
const listProps = useMemo(()=>({
|
||||||
|
items: items.filter(item => item.name.includes(filter)),
|
||||||
|
onItemClick: id => console.log('Clicked',id),
|
||||||
|
theme: 'dark'
|
||||||
|
}),[items,filter]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Memoized {...listProps} />
|
||||||
|
<button onClick={()=>setFilter('Bob')}>Click</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Memoized(props) {
|
||||||
|
console.log(props);
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h3 className={props.theme}>Memoized</h3>
|
||||||
|
{props.items.map((item) => (
|
||||||
|
<>
|
||||||
|
<li>
|
||||||
|
{item.name}
|
||||||
|
</li>
|
||||||
|
</>))}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
|
||||||
const [input,setInput] = useState('')
|
|
||||||
const [error,setError] = useState('')
|
|
||||||
const inputRef = useRef(null)
|
|
||||||
|
|
||||||
const addTask = () => {
|
|
||||||
if (input.trim() === '') {
|
|
||||||
setError('Пустой текст задачи')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setError('')
|
|
||||||
dispatch({
|
|
||||||
type: 'ADD_TASK',
|
|
||||||
payload: {id: Date.now(), text: input.trim(), complite: false }
|
|
||||||
})
|
|
||||||
setInput('')
|
|
||||||
inputRef.current.focus()
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{padding: 20}}>
|
<>
|
||||||
<h1>Список дел</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
<h1>Hello, React!</h1>
|
||||||
<button onClick={addTask}>Добавить</button>
|
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
||||||
{error && <p style={{color: 'red'}}>{error}</p>}
|
</>
|
||||||
|
|
||||||
<TaskList tasks={tasks}
|
|
||||||
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
|
||||||
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,5 @@ 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