user11587840
user11587840

Reputation:

How to use a method from grand Parent component to child component using props in react native?

const GrandParent = (props) => {
  const close = () => {
    console.log("Pressed");
  }
  return (<Parent func={() => close()} />)
}

const Parent = (props) => {
  return (<Child onPress={ /**call close function from GrandParent component*/} />)
}

const Child = (props) => {
  return (<Button onPress={() => props.onPress} />)
}

I was able to call GrandParents component method from Parent component using the props, but I want to call it from the Child component and I can't seem to do it.

Upvotes: 1

Views: 92

Answers (2)

Saurabh Patel
Saurabh Patel

Reputation: 11

const GrandParent = (props) => {
  const close = () => {
    console.log("Pressed");
  }
 return (<Parent func={() => close()} />)
}

const Parent = (props) => {
  return (<Child onPress={()=> props.func()} fun1={()=> props.func()}/>)
}

const Child = (props) => {
  return (<Button onPress={()=> props.func1()} />)
}

Upvotes: 1

Titulum
Titulum

Reputation: 11456

It should work like this:

const GrandParent = (props) => {
  const close = () => {
    console.log("Pressed")
  }
  return (<Parent func={() => close()} />)
}

const Parent = (props) => {
  return (<Child func={() => props.func()} />)
}

const Child = (props) => {
  return (<Button onPress={() => props.func()} />)
}

Upvotes: 0

Related Questions