SyntaxStudy
Sign Up
Home React Reference useState()

useState()

hook React 16.8

Adds local state to a function component. Returns current state and a function to update it.

Syntax

const [state, setState] = useState(initialValue)

Example

javascript
import { useState } from 'react';

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

    return (
        <div>
            <p>Count: {count}</p>
            <button onClick={() => setCount(count + 1)}>+</button>
            <button onClick={() => setCount(count - 1)}>-</button>
            <button onClick={() => setCount(0)}>Reset</button>
        </div>
    );
}