C P Verma
C P Verma

Reputation: 330

How to call a function from other independent component in ReactJS

I am trying to call a function (component A) from different component (component K). Both component does not have any relationship between them.

below is some example code for my problem-

Component A

const ComponentA= () => {
const start =()=>{
        console.log('function called')
                  }
return ()
}

Component K

const ComponentK= () => {
              

    return (
           <img className='tour' src={tourIcon}  height={25} width={25} onClick={start} />
           )
    }

here in above example- i want to call a function of component A from component K (both are independent compoent-does not have any relationship) on onClick.

please help

Upvotes: 0

Views: 166

Answers (1)

HogasAndreiMarius
HogasAndreiMarius

Reputation: 513

Use react context. Define function in context. Use it wherever you want

https://reactjs.org/docs/context.html

Upvotes: 3

Related Questions