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

320
Visualizações
React: How to trigger event in parent component when onClick event happened in child component

I have the following parent and child components. I want to run the changeBackground function every time the button in the child component (<NewQuoteButton />) is clicked but I don't know how to pass this information to the parent component. Can anyone please help?

//parent component

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state ={
      primary: '',
      prmaryDark: ''
    }
  }

  changeBackground = () => {

    let randomNum = Math.floor(Math.random()*backgroundColors.length)
      this.setState = {
        primary: backgroundColors[randomNum].main,
        prmaryDark: backgroundColors[randomNum].dark
      }
    }
  

  render() { 
    return (
      <div className="App">
        <QuoteBox />
    </div>
    );
  }
}

//child component

export default function QuoteBox() {

    const [newQuote, setNewQuote] = useState('');
    const [author, setAuthor] = useState('');

    const fetchRandomQuote = () => {
    fetch(url)

    // some method
  }

  return (
    <div className="QuoteBox" >
        <Text quoteText={newQuote} author={author}  />
        <Banner>
            <Links />
            <NewQuoteButton onClick={fetchRandomQuote} className="NewQuoteButton Button" />
        </Banner>
    </div>
    );
  
}


about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

//parent component

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state ={
      primary: '',
      prmaryDark: ''
    }
  }

  changeBackground = () => {

    let randomNum = Math.floor(Math.random()*backgroundColors.length)
      this.setState = {
        primary: backgroundColors[randomNum].main,
        prmaryDark: backgroundColors[randomNum].dark
      }
    }
  

  render() { 
    return (
      <div className="App">
        <QuoteBox handleColorChange={this.changeBackground} />
    </div>
    );
  }
}

//child component

export default function QuoteBox(props) {

    const [newQuote, setNewQuote] = useState('');
    const [author, setAuthor] = useState('');

    const fetchRandomQuote = () => {
props.handleColorChange();
    fetch(url)

    // some method
  }

  return (
    <div className="QuoteBox" >
        <Text quoteText={newQuote} author={author}  />
        <Banner>
            <Links />
            <NewQuoteButton onClick={fetchRandomQuote} className="NewQuoteButton Button" />
        </Banner>
    </div>
    );
  
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If you just want to run your changeBackground function when the button is clicked then call the function in your fetchRandomQuote function.

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