Every Project have some situation in call function one component from another component in react. In this case many UI developer don’t know how to call a parent component function from child component. But don’t worry about this, because we are going to explain you how to call parent component function from child component.In previous article you have already learn about how to pass value parent to child component and child to parent component. Then after easy to understand this topic.

Call function one component from another component in react

First you need to create OneComponent.js file and write your function in this component. As like below code.

Second thing, Also you need to create AnotherComponent.js and write your code for AnotherComponent. As like below code.

After that you will go to OneComponent.js Component for import AnotherComponent and use this imported component inside render and send a function as a props for AnotherComponent.

Final code of OneComponent.js file.

Go to AnotherComponent.js for call a function of OneComponent, You can see inside render, onClick event on called function of OneComponent.

Final code of AnotherComponent.js file.

Summery : Write a function in first component and same function called from another component. Passing function as props from first component to another component. Function is calling onClick event from another component.

Categories: React

Leave a Reply

Be the First to Comment!

Notify of
Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.