0

I have a project NextJS. I tried log a text at the component and relize that there are always 2 lines of my log. I am wondering that my component is render 2 times, it maybe affect the performance.

My component here:

const PostForm = () => {
    console.log("my log");

    return (<div>abc</div>);
}
export default PostForm;

Logs in my browser:

enter image description here

Hai Nguyen
  • 111
  • 1
  • 8

2 Answers2

4

That's behaviour included in React in development mode. It re-renders everything twice to check for unhandled side effects and warn you about them.

Read more about it here

Eldar B.
  • 1,097
  • 9
  • 22
-1

If you need to have full control about side-effects you should read about the useEffect() hook. Here is a really good explanation: https://dmitripavlutin.com/react-useeffect-explanation/