React | React Forms | Question 10

Last Updated :
Discuss
Comments

What will happen when the user clicks the button in the following code?

JavaScript
import React, { useState } from "react";

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

    const increment = () => {
        setCount(count + 1);
    };

    return (
        <div>
            <h1>{count}</h1>
            <button onClick={increment}>Increment</button>
        </div>
    );
}

The count will increment by 1 each time the button is clicked.

The count will remain at 0 because the state isn't updating correctly.

The code will throw an error due to a missing dependency in useState()

The count will decrement by 1 each time the button is clicked.

Share your thoughts in the comments