Why does React render the page two times?

vikram-s-narayan

In the following minimum working example of App.js:

import { useState, useEffect } from "react";


export default function App() {
  const [isShown, setIsShown] = useState(true);
  return (
    <>
      <button onClick = {() => setIsShown(!isShown)}>
        {isShown? 'Hide Counter' : 'Show Counter'}
      </button>
      {isShown? <Counter /> : null}
    </>
  );
}

function Counter(){
  const [count, setCount] = useState(0);
  const [bool, setBool] = useState(false);

  useEffect(() => {
    console.log('render');
  });
  
  useEffect(() => {
      console.log('mounted');
    }, []);
  return (
    <div className="counter">
      <button onClick={() =>setBool(!bool)}>Re-Render</button>
      <button onClick={() =>setCount(count + 1)}>Increment</button>
      <p> Count: {count}</p>
    </div>
  );
}

I get two sets of console.logs (i.e. the messages "render" and "mounted" show up twice) whenever I refresh the page or when I click on "Hide Counter" and then click on "Show Counter". My expectation is that "render" and "show" should show up only once. In the current case, this seems to imply that the components are rendering two times every time the page loads. I've tested this on Firefox and Chrome and find the same behavior in both.

Why is this and how can I begin to debug this?

Farnam H

This is because of React strict mode, if you remove it from your index file, you don't get two renders.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why React does not render component when I redirect to product page

why does react js renders two times when setting a state

Import React Does not Render Page

React: Help please ==> Why console log is printing two times on first render

React Router: why does nested route make react render on same page

why does Twig render blank page?

React does not render correct on page refresh

Why does call glBindBuffer() two times?

React js component re-render two times in scroll event

why does React counter component print two times console.log?

Why does a simple React Component render twice?

Why does React expression not render component?

Why does this component render twice in react?

Why does React not render my array of components?

React Router Issue: React render two components in the same page

Component render two times (reactJS)

Why my smart phone does not render Github page anymore?

Why react new page render from the bottom of the screen?

Why doesn't react re-render the page on button click?

Render a react component n times

React render button 'x' of times

Render react component multiple times

React Paginate Second Instance on same page does not re render

React router - page does not render until I do a browser refresh

React router does not render starting page component properly

Why does the user have to enter their correct credentials two times?

Why does my find command get executed two times?

Why does the 'init' method run in two times by the same object?

Why does render triggered only once every two keydowns?