Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda