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} />)
}
我可以使用props
从Parent
组件调用GrandParent
的组件方法,但我想从Child
组件调用它,但我似乎无法做到
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()} />)
}
它应该这样工作:
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()} />)
}