I have been created a new React application using create-react-app
, and i make a test with render counter.
Consider the following code:
import React, { useState } from "react";
let render = 0;
export default function App() {
const [counter, setCounter] = useState(0);
render++;
return (
<div>
<button
onClick={() => setCounter(counter + 1)}
>{`Clicked ${counter} times`}</button>
<p>{`Render counter: ${render}`}</p>
</div>
);
}
At my logic the render
variable at the first time need be start with 1, but... it's start with 2, and after i click on the button the render counter go to 4 not 3.
In my knowledge the render counter should change after each render when the state change, and state change 1 time per click, can someone explain me what's wrong? or why it's occours?
Sandbox: https://codesandbox.io/s/little-sky-squ82?file=/src/App.js
Thanks!