Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda