Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac5d9220e | |||
| 23db798577 | |||
| c99a045400 |
+65
-46
@@ -1,60 +1,79 @@
|
|||||||
import { memo, useState, useMemo } from 'react'
|
import { useState, createContext, useContext } from 'react'
|
||||||
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
function memoize(fn) {
|
const UserContext = createContext('')
|
||||||
const cache = new Map();
|
|
||||||
return function(...args) {
|
function UserProvider({ children }) {
|
||||||
const key = JSON.stringify(args);
|
const [name,setName] = useState('');
|
||||||
if (cache.has(key)) {
|
|
||||||
return cache.get(key);
|
const toggleName = (newName) => {
|
||||||
}
|
setName(newName);
|
||||||
const result = fn.apply(this, args);
|
|
||||||
cache.set(key,result);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function fibonachi(n, memo={}) {
|
|
||||||
if (n in memo) {console.log(n);return memo[n];}
|
|
||||||
if (n <= 1) return 1;
|
|
||||||
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
|
|
||||||
return memo[n];
|
|
||||||
}
|
|
||||||
|
|
||||||
const MyComponent = ({ value }) => {
|
|
||||||
console.log('Rendring');
|
|
||||||
return <div>{value}</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (
|
||||||
<>
|
<UserContext.Provider value={{name, toggleName}}>
|
||||||
<Child>
|
{children}
|
||||||
{stableChild}
|
</UserContext.Provider>
|
||||||
</Child>
|
)
|
||||||
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
}
|
||||||
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
|
||||||
</>
|
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() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button onClick={() => toggleName('')}>Выход</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Toolbar() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
return(
|
||||||
|
<div className={styles.container}>
|
||||||
|
<h3>Панель пользователя</h3>
|
||||||
|
{name && `Привет ${name}`}
|
||||||
|
{name ? <ExitButton /> : <Form /> }
|
||||||
|
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Page() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
||||||
|
<Toolbar />
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<Parent />
|
<UserProvider>
|
||||||
</>
|
<Page />
|
||||||
|
</UserProvider>
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
.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,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