Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

side effect, useEffect

// 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
Comment

side effect, useEffect, return

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>
      })}
    </>
  )
}
Comment

side effect, useEffect

useEffect(() => {
  console.log('This is my side effect')

  return () => {
    console.log('This is my clean up')
  }
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: Object Property Shorthand javascript 
Javascript :: javaScript getMonth() Method 
Javascript :: react native image from web 
Javascript :: interactive svg javascript 
Javascript :: joi validate 
Javascript :: flutter post request 
Javascript :: Detect Pangram 
Javascript :: like dislike node js 
Javascript :: how to call mixin in vuex 
Javascript :: react fetch data in for loop 
Javascript :: how to rename zip file nodejs 
Javascript :: ForEach Element with Function or Lambda 
Javascript :: array.length in mongoose query 
Javascript :: async await react stackoverflow 
Javascript :: redirect all routes to main component vue 
Javascript :: openstreetmap api example javascript 
Javascript :: Does my number look big in this? js 
Javascript :: npm modal 
Javascript :: ubuntu apps to install 
Javascript :: convert string to boolean in javascript 
Javascript :: como checar valor do input checkbox angular 
Javascript :: jquery numeric validation 
Javascript :: html video time 
Javascript :: upload bloob javascript 
Javascript :: object loop javascript 
Javascript :: lowercase 
Javascript :: mapbox add a leaflet marker with popup 
Javascript :: Clone Array Using Spread Operator 
Javascript :: react router v6 
Javascript :: node js 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =