I have two components and I’m struggling to create e function in the first component (by clicking a certain button) and the effect to be in the second component (changing the text on the screen). (I’m new to React and I’m not quite sure how to approach this)
All I want is to create a function on the 3 buttons (daily, weekly and monthly) in the Main component and change the text in the Content component into the <div>
with the "progress" className for <h1>
respective <h5>
This is the first component:
export default function Main() {
return (
<main>
<div className="second-card">
<div className="main-card">
<img
className="avatar-logo"
src="/images/image-jeremy.png"
alt="Avatar"
/>
<div className="main-card-details">
<p>Report for</p>
<h1>Jeremy Robson</h1>
</div>
</div>
<div className="stats">
<button className="daily">Daily</button>
<button className="weekly">Weekly</button>
<button className="monthly">Monthly</button>
</div>
</div>
</main>
);
}
This is the second one:
export default function Content(props) {
return (
<div className="dashboard-card">
<div className="icons">
<img className="card-icon" src={props.img} alt="activity icon" />
</div>
<div className="card-details">
<div className="card-activity-title">
<p>{props.activity}</p>
<img
className="ellipsis-icon"
src={props.cardIcon}
alt="Ellipsis card icon"
/>
</div>
<div className="progress">
<h1>{props.WeeklyHours}</h1>
<h5>{props.Weeklydate}</h5>
</div>
</div>
</div>
);
}
This is what I tried, but is basically creating a new card and not modifying the existing ones
// import React, { useState } from "react";
// import Content from "./Content";
export default function Main() {
// const [contentData, setContentData] = useState({
// DailyHours: "0 hours",
// DailyDate: "Default Date",
// });
// function changeMessage1() {
// setContentData({
// DailyHours: "10 hours",
// DailyDate: "Date 1",
// });
// }
// function changeMessage2() {
// setContentData({
// DailyHours: "20 hours",
// DailyDate: "Date 2",
// });
// }
// function changeMessage3() {
// setContentData({
// DailyHours: "30 hours",
// DailyDate: "Date 3",
// });
// }
return (
<main>
<div className="second-card">
<div className="main-card">
<img
className="avatar-logo"
src="/images/image-jeremy.png"
alt="Avatar"
/>
<div className="main-card-details">
<p>Report for</p>
<h1>Jeremy Robson</h1>
</div>
</div>
<div className="stats">
<button className="daily">Daily</button>
<button className="weekly">Weekly</button>
<button className="monthly">Monthly</button>
</div>
</div>
{/* <Content
WeeklyHours={contentData.DailyHours}
Weeklydate={contentData.DailyDate}
/> */}
</main>
);
}
2
Answers
React allows components to communicate with one another by lifting the state up to a common ancestor, the
Main
component in our example, and passing down the required state and functions to the child components as props. To add this capability, you can change your code as follows:With this change, the status of the chosen interval (
daily
,weekly
, ormonthly
) is stored in theMain
component, which changes it in response to button presses. This changed state is sent to theContent
component as props, which then shows the appropriate data.Ensure that the rendering logic in the
Content
component is adjusted to display theWeeklydate
andWeekly Hours
props that were received in the desired way.You can create a data object outside the component with all intervals and values and use it like this.