如何在react native中使用props来使用从grand-Parent组件到child组件的方法


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} />)
}

我可以使用propsParent组件调用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()} />)
}

相关内容

  • 没有找到相关文章

最新更新