javascript - 访问嵌套子组件中的父回调函数
问题描述
我正在尝试通过在 React Native 的顶级组件中实现此模式来创建全局可访问的模式。为此,我尝试使用回调函数扩展 SwitchNavigator(基于此链接),并通过导航道具使其可访问,但子组件似乎无法访问回调。如何使所有子组件都可以访问回调?
我当前的导航结构如下图所示(假设我尝试从 Leaf Component 访问回调):
- 扩展开关导航器
- MyTabNavigator
- 堆栈导航器(选项卡 1)
- 叶组件
- 选项卡 2
- 选项卡 3
- 堆栈导航器(选项卡 1)
- 其他组件
- MyTabNavigator
下面的片段显示了我当前的逻辑。
在 App.js 中:
const SwitchNavigatorComponent = createSwitchNavigator({
TabNavigator: MyTabNavigator,
OtherComponent: MyOtherComponent
})
// Extending the Switch Navigator component
class ExtendedSwitchNavigator extends Component {
static router = SwitchNavigatorComponent.router;
componentDidMount(){
this.setState({
modalVisible: false
})
}
presentModal = (status) => {
this.setState({modalVisible: {status}});
}
render(){
const { navigation } = this.props;
return(
<SwitchNavigatorComponent
navigation={navigation}
presentModal={this.presentModal} />
... (conditionally render modal)
)
}
}
const App = createAppContainer(ExtendedSwitchNavigator);
export default App;
叶组件:
export default class LeafComponent extends Component {
constructor(props) {
super(props);
console.log(this.props)
}
componentDidMount() {
this.props.presentModal(true);
}
}
尝试使用 访问回调时this.props.presentModal(true)
,出现错误
this.props.presentModal 不是一个函数。(在 'this.props.presentModal(true)' 中,'this.props.presentModal' 未定义)
props
当我在叶子组件的构造函数中打印时,我得到
Object {
"navigation": Object {
"actions": Object {
"dismiss": [Function dismiss],
"goBack": [Function goBack],
"navigate": [Function navigate],
"pop": [Function pop],
"popToTop": [Function popToTop],
"push": [Function push],
"replace": [Function replace],
"reset": [Function reset],
"setParams": [Function setParams],
},
"addListener": [Function addListener],
"dangerouslyGetParent": [Function anonymous],
"dismiss": [Function anonymous],
"dispatch": [Function anonymous],
"emit": [Function emit],
"getChildNavigation": [Function getChildNavigation],
"getParam": [Function anonymous],
"getScreenProps": [Function anonymous],
"goBack": [Function anonymous],
"isFocused": [Function isFocused],
"navigate": [Function anonymous],
"pop": [Function anonymous],
"popToTop": [Function anonymous],
"push": [Function anonymous],
"replace": [Function anonymous],
"reset": [Function anonymous],
"router": undefined,
"setParams": [Function anonymous],
"state": Object {
"key": "...",
"routeName": "Leaf1",
},
},
"screenProps": undefined,
}
其中不包含有关 presentModal 的任何内容。
[更新] 根据@Gaël S 的要求,这里是 ExtendedSwitchNavigator 和 Leaf 之间的组件。
const Tab1StackNavigator = createStackNavigator({
LeafComponent: LeafComponent
})
const MyTabNavigator = createBottomTabNavigator({
Tab1: Tab1StackNavigator,
Tab2: Tab2Screen,
Tab3: Tab3Screen
})
解决方案
您可以使用screenProps
将属性传递到所有屏幕:
<SwitchNavigatorComponent
navigation={navigation}
screenProps={{presentModal: this.presentModal}} />
然后每个屏幕都可以通过this.props.screenProps.presentModal
.
有关更多详细信息,请参阅文档
推荐阅读
- asp.net-core - 您是否从 ASP.Net Core 的存储库中返回 ObjectResult
- javascript - EmberJS 和 Web 组件:在初始化程序中扩展现有的 HTML 标记(可能吗?)
- java - 抛出异常时如何避免在返回值上延迟 NPE?
- python - 使用 tf-idf 的文档之间的余弦相似度和 TS-SS 相似度 - Python
- javascript - 尝试使用 JSON - 我做错了什么?
- html - 为什么我的输入宽度会增长这么多?
- c# - Azure 上是否有通用日志记录服务?
- bintray - 无法访问 Bintray 帐户
- python - 我应该如何使用 pip 安装软件包?
- c# - 如何在 ASP.NET 中将值从视图传递到存储库?