Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef33d16373 |
+39
-58
@@ -1,78 +1,59 @@
|
|||||||
import { useState, createContext, useContext } from 'react'
|
import { memo, useState, useMemo } from 'react'
|
||||||
import styles from './App.module.scss'
|
|
||||||
|
|
||||||
const UserContext = createContext('')
|
function memoize(fn) {
|
||||||
|
const cache = new Map();
|
||||||
function UserProvider({ children }) {
|
return function(...args) {
|
||||||
const [name,setName] = useState('');
|
const key = JSON.stringify(args);
|
||||||
|
if (cache.has(key)) {
|
||||||
const toggleName = (newName) => {
|
return cache.get(key);
|
||||||
setName(newName);
|
|
||||||
}
|
}
|
||||||
|
const result = fn.apply(this, args);
|
||||||
return (
|
cache.set(key,result);
|
||||||
<UserContext.Provider value={{name, toggleName}}>
|
return result;
|
||||||
{children}
|
|
||||||
</UserContext.Provider>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function Form() {
|
|
||||||
const {name, toggleName} = useContext(UserContext)
|
|
||||||
const [newName,setNewName] = useState('');
|
|
||||||
const handleSubmit = (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
toggleName(event.target[0].value)
|
|
||||||
}
|
}
|
||||||
const handlerChange = (event) => {
|
|
||||||
setNewName(event.target.value);
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
<form onSubmit={handleSubmit}>
|
|
||||||
<input type='text' value={newName} onChange={handlerChange} />
|
|
||||||
<button type='submit'>Войти</button>
|
|
||||||
</form>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function ExitButton() {
|
function fibonachi(n, memo={}) {
|
||||||
const {name, toggleName} = useContext(UserContext)
|
if (n in memo) {console.log(n);return memo[n];}
|
||||||
|
if (n <= 1) return 1;
|
||||||
return (
|
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
|
||||||
<button onClick={() => toggleName('')}>Выход</button>
|
return memo[n];
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Toolbar() {
|
const MyComponent = ({ value }) => {
|
||||||
const {name, toggleName} = useContext(UserContext)
|
console.log('Rendring');
|
||||||
return(
|
return <div>{value}</div>
|
||||||
<div className={styles.container}>
|
|
||||||
<h3>Панель пользователя</h3>
|
|
||||||
{name && `Привет ${name}`}
|
|
||||||
{name ? <ExitButton /> : <Form /> }
|
|
||||||
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Page() {
|
const MemoizedComponent = memo(MyComponent);
|
||||||
|
|
||||||
|
const Child = memo(({ children }) => {
|
||||||
|
console.log('Child component rendred');
|
||||||
|
return <div>{children}</div>;
|
||||||
|
});
|
||||||
|
|
||||||
|
const Parent = () => {
|
||||||
|
const [count,setCount] = useState(0);
|
||||||
|
const [text, setText] = useState('');
|
||||||
|
|
||||||
|
const stableChild = useMemo(() => <span>Memo text</span>,[]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<>
|
||||||
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
<Child>
|
||||||
<Toolbar />
|
{stableChild}
|
||||||
</div>
|
</Child>
|
||||||
|
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
||||||
|
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<UserProvider>
|
<Parent />
|
||||||
<Page />
|
|
||||||
</UserProvider>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
.container {
|
|
||||||
height: 500px;
|
|
||||||
border: 1px solid black;
|
|
||||||
border-radius: 10px;
|
|
||||||
background-color: lightblue;
|
|
||||||
}
|
|
||||||
.item {
|
|
||||||
font-size: 16pt;
|
|
||||||
border: 1px dotted gray;
|
|
||||||
/*margin: 5px 10px;
|
|
||||||
padding: 10px;*/
|
|
||||||
border-radius: 5px;
|
|
||||||
}
|
|
||||||
@@ -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