Cesare’s Newsletter

Share this post

Adding a method to a class component

cesare.substack.com

Discover more from Cesare’s Newsletter

Weekly insights about increasing ROI of business websites.
Continue reading
Sign in

Adding a method to a class component

How to pass class component method to child component in React

Cesare Ferrari
Mar 9, 2020
Share this post

Adding a method to a class component

cesare.substack.com
Share

Sometimes we need to add a method to a class component and pass it down to its child component.
For example, we may have a GroceryList component that has a child component called GroceryItem.
We define a toggleItem() method in GroceryList that we want to trigger from GroceryItem. How do we do that?

Let's first define the method in the parent component.
The way we add a method to a class component is to simply create an identifier inside the class definition and assign an arrow function to it.
The identifier will be the name of the method we call.

class GroceryList extends React.Component {

  toggleItem = itemId => {
    // perform a toggle action
  }

}

To use this method in the child component, we need to pass it down via props:

<GroceryItem item={item} toggleItem={this.toggleItem} />

Inside the child component we then use this method in an onClick event:

<div onClick={ () => {props.toggleItem(props.item.id)} } />
  Click me
</div>

When we click on the <div> element now we trigger the onClick event that will call toggleItem() in the parent component, passing the item id as an argument.

Share this post

Adding a method to a class component

cesare.substack.com
Share
Previous
Next
Comments
Top
New

No posts

Ready for more?

© 2023 Cesare Ferrari
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing