Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
how to replace h2 with div on same page after on click with h2 (and without using component)

I am very new to stack overflow and this is also my first post so I am not sure if this question is specific/detailed enough but...: I am trying to figure out how to replace the h2 element 'Unpack the Week' with the div between the comments. For now I am trying to avoid rendering a new component and simply just have a div hiding until the h2 element is clicked to view the information inside of that div (my project is all on one page/one path). I created a weekToggle function attached to the h2 but I dont think its the right direction of what I want.

enter code here
    const weekToggle = () => {
        console.log('clicked');
        return (
            <div>
                <img src="" alt="" />
                <h4>Monday</h4>
                <img src="" alt="" />
                <h4>75˚/65˚</h4>
            </div>
        )
    }

    return (

        <div>
            <div>
                <img src={unpackedImg(cityConditions)} alt="" />
                <div>
                    <img src={iconImg(cityConditions)} alt="" />
                    <h1>{cityName}</h1>
                    <h2>{cityTemp}</h2>
                    <div>
                        <div>
                            <h3>{cityConditions}</h3>
                            <p>Conditions</p>
                        </div>
                        <div>
                            <h3>{cityWind}mph</h3>
                            <p>Wind speed</p>
                        </div>
                        <div>
                            <h3>{cityHumidity}%</h3>
                            <p>Humidity</p>
                        </div>
                    </div>
                </div>
            </div>
            <h1>{cityName} unpacked!</h1>
            <div>
                {/* after clicking h2 element 'Unpack the Week' replace h2 with this div */}
                {/* ---------------------------------------------------------------------*/}
                <h2 onClick={weekToggle}>Unpack the Week</h2>
                <div>
                    <img src="" alt="" />
                    <h4>Monday</h4>
                    <img src="" alt="" />
                    <h4>75˚/65˚</h4>
                </div>
                {/* ---------------------------------------------------------------------*/}
            </div>
        </div>

    )
}

Weather.propTypes = {
    weather: PropTypes.object
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm assuming you want to build collapsible element with details regarding temperatures for the whole week. The proper way to do it in React is using the hook useState which holds the information whether the details are visible or not. I re-designed your component a bit to show the idea.

import {useState} from "react";

function CityWeather ({cityName, cityTemp, cityConditions}) {

    const [showDetails, setShowDetails] = useState(false);

    return (

        <div>
            <h1>{cityName}</h1>
            <h2>{cityTemp}
                <span style={{cursor: 'pointer'}} onClick={() => setShowDetails(!showDetails)}>
                    &nbsp;{ showDetails ? <>&#x25B2;</> : <>&#x25BC;</> }
                </span>
            </h2>
            { showDetails &&
                <div>
                    <div>
                        <h4>Monday</h4>
                        <h4>75˚/65˚</h4>
                    </div>
                    <div>
                        <h4>Tuesday</h4>
                        <h4>76˚/66˚</h4>
                    </div>
                    <div>
                        <h4>Wednesday</h4>
                        <h4>77˚/67˚</h4>
                    </div>
                </div>
            }
            <div>
                <div>
                    <h3>{cityConditions}</h3>
                    <p>Conditions</p>
                </div>
            </div>
        </div>
    )
}

use this component in your App like this:

<CityWeather cityName="London" cityTemp="75˚" cityConditions="fair"/>

Feel free to comment if you need any explanation.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!