41
loading...
This website collects cookies to deliver better user experience
We can use React Hooks inside function components only.
Only call hooks at top level. You cannot call hooks inside loops, conditions, or nested functions, they should always be called at the top of function component.
You cannot call hooks from regular JavaScript functions.
import { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
import { useState, useEffect } from "react";
import ReactDOM from "react-dom";
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
setTimeout(() => {
setCount((count) => count + 1);
}, 1000);
}, []);
return <h1>I've rendered {count} times!</h1>;
}