A
A
Alexey Alekseev2020-04-16 17:01:56
JavaScript
Alexey Alekseev, 2020-04-16 17:01:56

Why does a component with React.useState() render twice?

This code will print "App" to the console once.

import React from "react";
export default function App() {
  console.log('App');
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

And this twice
import React from "react";
export default function App() {
  console.log('App');
  const [foo, setFoo] = React.useState(1);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

One line added
const [foo, setFoo] = React.useState(1);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question