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

245
Views
How to get child function component method on parent class component at onClick in ReactJS

I have a Parent (Class Based Component) and a Child (Function Based Component). I need the method of function based component, when I will click on (+) Button of Parent Class Component.

  • I have imported child component to parent component
  • I can't handle the click event in here of parent component

onClick={ }

Here is my Parent Class Based Component

import React from "react";
import Increment from "./Increment";

class Timer extends React.Component {

    //State
    state = {
        count: 0
    }
    render() {
        return (
                <div className="top">
                    
                    <span className="display">{this.state.count}</span>
                    <button className="btn btn-primary"  onClick={<Increment  />} >
                        +
                    </button>
                </div>
        )
    }
}
export default Timer

Here is my Child Function Component

import React, { Component } from "react";

class Increment extends Component {
    incrementTimer = props => {
        this.setState({ count: props.count + 1 })
    }
}
export default Increment
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I believe this article may shed some light on your issue: Lifting State Up - React Docs. I wouldn't suggest trying to update the state of Component #1 from inside Component #2. Take the increment logic from your Increment Component and make it a method on your Parent Component. Then pass the method as a prop on the Increment Component.

In Timer.js

import React, { Component } from "react";
import Increment from "./Increment";

class Timer extends Component {
  // The Constructor is necessary for adding the handleIncrement method
  // State should be initialized here as well.
  constructor(props) {
    // super(props) is required on class based component constructors
    super(props);
    this.state = { count: 0 };
    this.handleIncrement = this.handleIncrement.bind(this);
  }

  // This is the method
  handleIncrement() {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div className="top">
        <span className="display">{this.state.count}</span>
        {/* handleIncrement is the name of the prop that will be referenced inside
        the Increment.js Component. */}
        {/* this.handleIncrement is the method. */}
        <Increment handleIncrement={this.handleIncrement} />
      </div>
    );
  }
}

export default Timer;

In Increment.js

import React from "react";

// Putting (props) means this component is expecting a prop when
// its been imported and used as <Increment propGoesHere={value} /> in Timer.js
const Increment = (props) => {
  return (
    <button className="btn btn-primary" onClick={props.handleIncrement}>
      +
    </button>
  );
};

export default Increment;

As an aside, Class based components can be avoided altogether (because who wants to deal with the "this" keyword, constructors, and binding every method) by using the useState hook.

It would look something like: In Timer.js

import React, { useState } from "react";

const Timer = () => {
  const [count, setCount] = useState(0);

  const handleIncrement = () => {
    setCount(count + 1);
  };
  return (
    <div className="top">
      <span className="display">{count}</span>
      <button className="btn btn-primary" onClick={handleIncrement}>
        +
      </button>
    </div>
  );
};

export default Timer;
  • No need for a class to hold the state anymore, now any component can house state!
  • No more constructor, no need to bind methods, no more "this" keyword!
  • No need for the entire Incremement child Component
about 4 years ago · Juan Pablo Isaza Report

0

First of all

<button className="btn btn-primary"  onClick={<Increment  />} >
  +
</button>

This specific piece of code is impossible to make work. You cannot pass a component as a function to an onClick. Since I do not know the practical application of your question I have no clue what is exactly you are trying to achieve so here is just an option of how to make these specific components work.

import React from "react";
import Increment from "../components/Increment";

class Timer extends React.Component {
  //State
  state = {
    count: 0,
    mountCounter: false,
  };

  increaseCount = () => {
    this.setState({ count: this.state.count + 1, mountCounter: false });
  };

  render() {
    return (
      <div className="top">
        <span className="display">{this.state.count}</span>
        <button
          className="btn btn-primary"
          onClick={() => this.setState({ mountCounter: true })}
        >
          +
          {this.state.mountCounter && (
            <Increment increaseCount={() => this.increaseCount()} />
          )}
        </button>
      </div>
    );
  }
}
export default Timer;

import React, { Component } from "react";

class Increment extends Component {
  constructor(props) {
    super(props);
  }
  componentDidMount() {
    this.props.increaseCount();
  }
  render() {
    return null;
  }
}
export default Increment;
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!