For example:

import React, { useState } from "react";
import ReactDOM from "react-dom";

function FavoriteColor() {
const [color, setColor] = useState("blue");

return (
<>
<h1>My favorite color is {color}!</h1>
<button
type="button"
onClick={() => setColor("yellow")}
>Yellow</button>
<button
type="button"
onClick={() => setColor("blue")}
>Blue</button>
<button
type="button"
onClick={() => setColor("orange")}
>Orange</button>
<button
type="button"
onClick={() => setColor("violet")}
>Violet</button>
</>
);
}

ReactDOM.render(<FavoriteColor />, document.getElementById('root'));

We must import Hooks from react. The useState is used to track the application state.

Rules of Hooks:

Categorized in:

ReactJS

Tagged in:

, , , , , , , , , , , , , , ,

Share Article:

Leave a Reply

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock