最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does a simple React Component render twice? - Stack Overflow

programmeradmin2浏览0评论

I just started a new create-react-app project and noticed that react is rendering ponents twice! My react version in package.json is "react": "^16.13.1"

import React, { useRef } from "react";

const App = () => {
  const renders = useRef(0);
  console.log("renders: ", renders.current++);

  return (
    <div>
      Hello
    </div>
  );
};

This produces on first render:

renders: 0
renders: 0

Now if I add a button to increment state, each state change also produces two additional renders:

import React, { useRef } from "react";

const App = () => {
  const [count, setCount] = useState(0);
  const renders = useRef(0);
  console.log("renders: ", renders.current++);

  return (
    <div>
      <button onClick={() => setCount(count + 1)}>increment</button>
      <div>count: {count}</div>
    </div>
  );
};

This will result in:

//--- initial render
renders: 0
renders: 0
//--- first click
renders: 1
renders: 2
//--- second click
renders: 3
renders: 4
//--- third click
renders: 5
renders: 6

Is this normal or is this a bug in the latest version of react?

I just started a new create-react-app project and noticed that react is rendering ponents twice! My react version in package.json is "react": "^16.13.1"

import React, { useRef } from "react";

const App = () => {
  const renders = useRef(0);
  console.log("renders: ", renders.current++);

  return (
    <div>
      Hello
    </div>
  );
};

This produces on first render:

renders: 0
renders: 0

Now if I add a button to increment state, each state change also produces two additional renders:

import React, { useRef } from "react";

const App = () => {
  const [count, setCount] = useState(0);
  const renders = useRef(0);
  console.log("renders: ", renders.current++);

  return (
    <div>
      <button onClick={() => setCount(count + 1)}>increment</button>
      <div>count: {count}</div>
    </div>
  );
};

This will result in:

//--- initial render
renders: 0
renders: 0
//--- first click
renders: 1
renders: 2
//--- second click
renders: 3
renders: 4
//--- third click
renders: 5
renders: 6

Is this normal or is this a bug in the latest version of react?

Share Improve this question asked Aug 1, 2020 at 17:53 user3376065user3376065 1,1771 gold badge13 silver badges31 bronze badges 2
  • 3 It's because of strict mode. So, it will run twice in development not in production. – Bhojendra Rauniyar Commented Aug 1, 2020 at 18:02
  • Yeah I realized this after some additional research. Thank you. – user3376065 Commented Aug 1, 2020 at 18:06
Add a ment  | 

2 Answers 2

Reset to default 13

OK looks like I found out the reason. After inspecting index.js I found the following:

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

Looks like create-react-app now includes React.StrictMode which double invokes certain methods in development mode (not in production).

In addition to the StrictMode issue you found, I think when you're using a ref like that it creates a side-effect so you'd typically need to put it in useEffect to prevent it from rendering twice:

import React, { useState, useEffect, useRef } from "react";

const App = () => {
  const [count, setCount] = useState(0);
  const renders = useRef(0);
  useEffect(() => {
    // Every time the ponent has been re-rendered,
    // the counter is incremented
    console.log("renders: ", renders.current++);
  }); 


  return (
    <div>
      <button onClick={() => setCount(count + 1)}>increment</button>
      <div>count: {count}</div>
    </div>
  );
};

export default App;
发布评论

评论列表(0)

  1. 暂无评论