Skip to content

Instantly share code, notes, and snippets.

@devstefancho
Last active June 25, 2020 16:00
Show Gist options
  • Save devstefancho/b6edc6beee5b37d94cb4f4250c9879ee to your computer and use it in GitHub Desktop.
Save devstefancho/b6edc6beee5b37d94cb4f4250c9879ee to your computer and use it in GitHub Desktop.
When you want to find a state which one is affecting in re-rendering your component
// React-Hooks
import {useEffect, useRef} from 'react'
// Suppose you have three state : restaurant, address, weather. and you want to find which state is affecting in rendering
console.log('restaurant' , restaurant);
useEffect(()=>{
console.log('restaurant effect', restaurant);
},[restaurant]);
console.log('address' , address);
useEffect(()=>{
console.log('address effect', address);
},[address]);
console.log('weather' , weather);
useEffect(()=>{
console.log('weather effect', weather);
},[weather]);
// If you find the state, you can compare previous state with current state, using useRef.
// I guess address display "address effect" in console
const refAddress = useRef(address);
useEffect(()=>{
console.log(refAddress.current, address, refAddress === address)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment