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} />)
}
我可以使用GrandParent
从Parent
组件中调用props
的组件方法,但是我想从Child
组件中调用它,但是我不能似乎做到了。
答案 0 :(得分:1)
它应该像这样工作:
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()} />)
}
答案 1 :(得分:1)
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()} />)
}