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

321
Views
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 answers
Answer question

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 Report

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 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!