Last active
June 25, 2020 16:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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