React empty useEffect

Viewed 863

Even though I'm working with React for some time, I can't figure this out. Why is the empty UseEffect causing component to re-render? The thing is I get two console.logs. Tnx!

function App() {
  useEffect(() => {

  }, [])

  console.log('render')

  return (
    <h1>Hello world</h1>
  );
}
1 Answers

Empty useEffect subscribed to all the component's props.

useEffect(() => console.log('rerendered due to new props'));

Any useEffect works at least once after first component's render. So you can use it like componentDidMount.

If you want to use it like componentDidMount only, you have to pass an empty array as the second param.

useEffect(() => console.log('after first render'), []);

Subscribe to a specific prop:

useEffect(() => console.log(' after first render or message updated', props.message), [props.message]);
Related