4

I'm attempting to add an onClick to a component instance which changes the state of a useState hook, and prints it out to the console. However, nothing fires when the component instance is clicked.

I have tried wrapping it in a JSX div element and adding an onClick to the div which works in printing to the console, but the state is not updated as always shows an undefined.

The end goal is to pass the state to another component (Linked page) after click.

Here is my code.

Card.js

export default function Card(props) {
  return (
    <div className="card" style={{ background: `${props.background}` }}>
      <h2>{props.taskName}</h2>
      <p>{props.description}</p>
    </div>
  );
}

Home.js

import { Link } from "react-router-dom";
import { useState, useEffect } from "react";
import Card from "../components/Card";
import "../pages/Home.scss";
import ProfileButton from "../components/ProfileButton";
import Header from "../components/Header";
import "./PlayerPage";

export default function Home() {
  const date = new Date();
  const time = date.getHours();
  let timeOfDay;

  if (time < 12) {
    timeOfDay = "Morning";
  } else if (time >= 12 && time < 6) {
    timeOfDay = "Afternoon";
  } else {
    timeOfDay = "Evening";
  }

  const [task, setTask] = useState();

  useEffect(() => {
    console.log(task);
  })

 return (
  <>
   <Header />
    <main className="home-main">
     <div className="main-wrap">
      <section className="card-container">
       <h1>Good {timeOfDay}</h1>
        <h1>Choose Your Work Style</h1>

         <div className="card-wrap">
          <Link to="/PlayerPage">
           <Card onClick={() => {console.log("hey") }}
            taskName="Short Task"
            description="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
         />
          </Link>
     </div>
</>
);
}

PlayerPage.js - (Component which needs to display updated state)

export default function PlayerPage() {
  return (
    <>
      <Header />
      <main className="player-page-main">
        <div className="player-page-main-wrap">
          <div className="timer-title-wrap">
            <Timer />
            <h2 className="task-title">{Updated state here}</h2>
          </div>
        </div>
      </main>
    </>
  );
}
6
  • What in the Card component do you want to respond to being clicked? Commented Sep 18, 2021 at 6:50
  • As per the chosen answer, the card component is now clickable. However, state still doesn't update Commented Sep 18, 2021 at 7:24
  • It's unclear how you are validating the state update. Can you update your question to include a complete Home component so we can see the state and how Card is rendered? I see the state update in this running codesandbox. Oh I see, you are wanting to pass the task state on to PlayerPage component? What routing/navigation library are you using? I think this new issue is enough off-topic from this one it warrants a new stackoverflow post. Commented Sep 18, 2021 at 7:33
  • I'm using BrowserRouter & I agree. Will make a new post. Thanks for all the help Commented Sep 18, 2021 at 7:43
  • Feel free to ping (@) me here with a link to the new post, or ping (@) me in a comment in the new post, and I'll take a deeper look. Commented Sep 18, 2021 at 7:45

1 Answer 1

2

The Card component needs to attach the passed onClick handler prop to a DOM element.

Example:

export default function Card(props) {
  return (
    <div
      className="card"
      style={{ background: `${props.background}` }}
      onClick={props.onClick}
    >
      <h2>{props.taskName}</h2>
      <p>{props.description}</p>
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhh I see why this makes sense now. Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.