useState

Syntax

1
const [state, setState] = useState(optionalInitialState);
Copied!
Where:
  1. 1.
    const [state,setState] : Return of useState, the arguments allow reading and updating of the state associated with the hook instance.
    • state : Current state.
    • setState: Current status updater.
  2. 2.
    useState( optionalInitialState ): Function that associates the state to the webcomponent:
    • optionalInitialState: Optional parameter that defines the initial state associated to the hook instance, If optionalInitialState is a function it will be executed in order to obtain the initial state only at the moment of the hook instance for the first time.

Example

1
function MyComponent() {
2
const [count, setCount] = useState(0);
3
return <host onclick={() => setCount(count + 1)}> {count} </host>;
4
}
Copied!
Last modified 3mo ago
Copy link
Edit on GitHub