Skip to content

Instantly share code, notes, and snippets.

@MichaelDimmitt
Created March 6, 2021 20:20
Show Gist options
  • Save MichaelDimmitt/6534f9e91183dba9825ede604e064394 to your computer and use it in GitHub Desktop.
Save MichaelDimmitt/6534f9e91183dba9825ede604e064394 to your computer and use it in GitHub Desktop.
quick poor mans, way for react to tell how many of these components exist by rendering one time.

Everyone seems to know this, but here is a good refresher in case I ever forget.

  useEffect(() => { console.log('I am a component, 1 render') }, [true]);

Pointed empty array works you dont need true, My paranoid self thought that I read in the documentation that empty array would track all. This was not the case.

  useEffect(() => { console.log('I am a component, 1 render') }, []);
@harmolipi
Copy link

harmolipi commented Dec 8, 2021

const fn = () => { console.log('component rendered') };
const these = 'test';
const states = 'test';
useEffect(fn); // all state
useEffect(fn, []); // no state
useEffect(fn, [these, states]);

https://twitter.com/ryanflorence/status/1125041041063665666

@MichaelDimmitt
Copy link
Author

MichaelDimmitt commented Dec 8, 2021

import { useEffect } from 'react';
// ...
  const  { var1, var2, var3 } = props
  
  useEffect(() => {
    console.log('tell me what loaded first time', { var1, var2, var3 })
  }, [])
  useEffect(() => {
    console.log('tell me what changed plz ', { var1, var2, var3 })
  })
  
  /*
  useEffect(() => { // cleanup yo use effects.
    effect
    return () =>  cleanup
  }, [input])
*/

https://gist.github.com/clintonlunn/410e6db90866c5e2f9cff70a32e79afd

@harmolipi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment