React Hooks: useEffect

In react hooks we can utilize useEffect hook to add codes that can “observe” the other variables or codes and run the codes inside its callback function.

An example here

import React from "react";

export default function App() {
  const [buttonToggle, setButtonToggle] = React.useState(false);

  React.useEffect(() => {
  }, [buttonToggle]);

  return (
    <div className="App">
        style={{ backgroundColor: buttonToggle ? "transparent" : "yellow" }}
        onClick={() => {

The useEffect here “observe” the changes in buttonToggle variable and when the variable value change it will display an alert in browser window.



Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.