FAQ: Components and Advanced JSX - Components Recap

This community-built FAQ covers the “Components Recap” exercise from the lesson “Components and Advanced JSX”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn ReactJS: Part I

FAQs on the exercise Components Recap

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

So, with regards to Components and Advanced JSX, 6/6:

If you would like to practice your learned skills, consider these challenges:
Add logic to the MyQuote component to produce different quotes based on different conditions.
Add elements onto the screen that can respond to events such as clicking or hovering. Make sure to define event handlers for them within the component.

Any thoughts about the following? Hovering does not work, everything else does:

import React, { useState } from 'react'; //to use state & react functional event handler components

function MyQuote() { //definition of MyQuote component as a function with 3 state variables using hook = useState
  const [quote, setQuote] = useState("Initial quote");
  const [message, setMessage] = useState("");
  const [condition, setCondition] = useState(false);

//now 3 event handlers
  const handleClick = () => {
    setQuote("New quote after clicking the button!");
  };

  const handleMouseOver = () => {
    setMessage("You are hovering over the paragraph!");
  };

  const handleToggleCondition = () => {
    setCondition(!condition);
  };

  let displayedQuote = quote;

  if (condition) {
    displayedQuote = "Alternate quote";
  }

  return (
    <div>
      <blockquote>
        <p>{displayedQuote}</p>
        <cite>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Susan_Sontag">
            Susan Sontag
          </a>
        </cite>
      </blockquote>
      <button onClick={handleClick}>Change Quote</button>
      <p onMouseOver={handleMouseOver}>{message}</p>
      <button onClick={handleToggleCondition}>Toggle Condition</button>
    </div>
  );
}

export default MyQuote;

so another go to the same thing but now everything works with some more functionality added. Anybody there?:

import React, { useState } from "react"; //to use state & react functional event handler components

function MyQuote() {
  //definition of MyQuote component as a function with 3 state variables using hook = useState, now 4 state variables

  const quotes = [
    "Initial quote",
    "New quote after clicking the button!",
    "Another inspiring quote",
    "Yet another insightful quote"
  ];
  
  const [quoteIndex, setQuoteIndex] = useState(0);
  const [condition, setCondition] = useState(false);
  const [hoverQuote, setHoverQuote] = useState("Hover over me to see a quote!");
  const [clickCount, setClickCount] = useState(0);

  //before 3, now 4 event handlers
  const handleClick = () => {
    setQuoteIndex((prevIndex) => (prevIndex + 1) % quotes.length);
    setClickCount(clickCount + 1);
  };

  const handleToggleCondition = () => {
    setCondition(!condition);
  };

  const handleHoverQuote = () => {
    if (hoverQuote === "Hover over me to see a quote!") {
      setHoverQuote("This is a quote revealed by hovering!");
    } else {
      setHoverQuote("Hover over me to see a quote!");
    }
  };

  let displayedQuote = quotes[quoteIndex];

  if (condition) {
    displayedQuote = "Alternate quote";
  }

  return (
    <div>
      <blockquote>
        <p>{displayedQuote}</p>
        <cite>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Susan_Sontag">
            Susan Sontag
          </a>
        </cite>
      </blockquote>
      <button onClick={handleClick}>Change Quote</button>
      <button onClick={handleToggleCondition}>Toggle Condition</button>
      <p onMouseOver={handleHoverQuote}>{hoverQuote}</p>
      <p>Change Quote button clicked {clickCount} times</p>
    </div>
  );
}

export default MyQuote;