当我从BottomTabNavigator的子屏幕调用this.props.navigation.dangerouslyGetParent()
时,我得到undefined
。我确定这个标签页有父母,但是为什么会这样呢?我正在尝试在“标签”导航器级别保存设置,并将这些设置传递给子屏幕。可以更改这些设置的唯一孩子是SettingsScreen。
这是代码:
const Tab = createBottomTabNavigator();
export default class MyTabs extends React.Component {
constructor(props){
super(props)
}
render (){
return (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} options={{headerShown:false}}/>
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}
设置屏幕:
export default class SettingsScreen extends React.Component{
constructor(props){
super(props)
this.state = {evenLicensePlate:null}
}
saveSettingsButton = () =>{
this.props.navigation.dangerouslyGetParent() // <- is undefined
}