Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the render function in react is called twice when using the component strategy?

I am new to react and what to understand why the console.log is called twice when inside of the render function ?

import React, { Component } from "react";
import "./App.css";

class App extends Component {

  render() {
    console.log("Prints twice in console");
    return (
      <div className="App">

      </div>
    );
  }
}

export default App;

Where as if i dont extend from component and use function instead the console prints only once

import React from "react";
import "./App.css";
function App() {
  console.log("prints once");
  return <div className="App"></div>;
}

export default App;
like image 558
user1738360 Avatar asked Jul 31 '26 11:07

user1738360


1 Answers

Check your index.js in the ./src directory. I think it renders the App component in

<React.StrictMode>

Remove it and it will stop rendering the function twice.

Also you can check This

like image 160
Arnab Avatar answered Aug 03 '26 00:08

Arnab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!