// MOUNTED
// This is my side effect
// RE-RENDER 1:
// This is my clean up
// This is my side effect
// RE-RENDER 2:
// This is my clean up
// This is my side effect
// UN-MOUNT:
// This is my clean up
function WindowSizeList({ url }) {
const [windowWidth, setWindowWidth] = useState(window.innerWidth)
const [items, setItems] = useState([])
const updateWindowWidth = () => {
setWindowWidth(window.innerWidth)
}
useEffect(() => {
setItems(CustomApi.getList(url))
}, [url])
useEffect(() => {
window.addEventListener('resize', updateWindowWidth)
return () => {
window.removeEventListener('resize', updateWindowWidth)
}
}, [])
return (
<>
<div>Window Width: {windowWidth}</div>
{items.map(item => {
return <div key={item}>{item}</div>
})}
</>
)
}
useEffect(() => {
console.log('This is my side effect')
return () => {
console.log('This is my clean up')
}
})