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

68
Visualizações
set value on a click button from a function component to another const component React

All I want here is when I press the search button in Hero.jsx , I set a value to the guest constant in Hotelcards.jsx , any solutions ?

the guest value that I wanna set is on this file.

Hotelcards.jsx

import { Link } from "react-router-dom";
import React,{useState,useEffect} from 'react';
import styles from '../styles/HotelListCards/HotelCards.module.css';
import {Checkkin as checkkin}  from "./Hero";
import {Checkkout as checkkout}  from "./Hero";
import {rowss as rows } from "./Hero";
import {notavailableat as notavailableat } from "./Hero";
import {prices as prices } from "./Hero";

const HotelCards = ({ idroom , title, status = true, price, img  }) => {
    const [guests, setGuest] = useState('')

    const [qty, setTitle] = useState('')
    var total_price = 0;
    if(prices.length!==0){
        for (var i=0;i<prices.length-1;i++){
            total_price+=parseFloat(prices[i]);
        }

    }
};

And the button that will trigger the event of changing the value is on this file. Hero.jsx

import React, {useEffect, useState, useCallback} from 'react';
import styled from 'styled-components';
import homeImage from '../assets/booking-bg.jpg';
import styles from '../styles/HotelListCards/HotelCards.module.css';
import {differenceInDays, format} from "date-fns";

var Checkkin = 0;
var Checkkout= 1;
let notavailableat="";
let rowss=[];
let prices =[];
export {Checkkin,Checkkout,rowss,notavailableat,prices};

export default function Hero() {
    const [availdata, setavailData] = useState([]);
    const [isLoading, setIsLoading] = useState(false);
}
<div className="search">
    <button >Search</button>
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

We will need a bit more of your code to really understand how this is being implemented, however it appears to me you want your search button, the Hero component class, to pass up its values to the parent HotelCard. If that is the case, you can pass function handlers from HotelCard to Hero, hero can then call the function onClick, and set the data from the Hero inside the HotelCard using the callback function passed as a prop to the Hero Component. Hope this helps, the general idea above will work but you may need to change which component is which as its not a complete code base/implementation as you have posted.

about 4 years ago · Juan Pablo Isaza Relatório

0

So the solution was wrapping both siblings in a parent component and handle the state inside the parent component. Then you easily can share the state between any siblings that you want and use the state inside the child components. :

in the parent component :

function App() {
    const [selectedMode, setSelectedMode] = useState('open')
.
.
.

<>
            <NewNavbar />
              <Hero setSelectedMode={setSelectedMode} />
              <Services />
              <Options selectedMode={selectedMode}/>
              <ScrollToTop />
              <Footer />
              {/* <PaymentSummaryFinal1 />*/}
            </>

in the first sibling Hero:

export default function Hero({setSelectedMode}) {
    const onButtonClick=(mode)=>{
        setSelectedMode(mode)
    }
.
.
.
<button onClick={()=>onButtonClick('closed')} >Search</button>

and on the other sibling Options.jsx

export default function Options({selectedMode}) {
.
.
.

    return (
        <div className={`${styles.containers}`}>

            
                <div>
                    <HotelCards
                        mode={selectedMode} // pass it to the child of this sibling
                    />
                </div>
            )}

        </div>
    );

}

Thanks to Chad S in the comments.

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