Сделали кастомный хук
This commit is contained in:
+15
-2
@@ -1,10 +1,23 @@
|
|||||||
// удаляем все, оставляя лишь пустую функцию App ниже
|
import React, {useState, useEffect} from 'react'
|
||||||
|
|
||||||
|
function useWindowWidth() {
|
||||||
|
const [width,setWidth] = useState(window.innerWidth)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleResize = () => setWidth(window.innerWidth);
|
||||||
|
window.addEventListener('resize',handleResize);
|
||||||
|
return () => window.removeEventListener('resize',handleResize);
|
||||||
|
},[])
|
||||||
|
|
||||||
|
return width
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const width = useWindowWidth();
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!{width}</h1>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user