在反应导航 5.x 上动态更改标题标题
问题描述:
我最近更新了我的项目以响应导航 5.x.在早期版本中,我们使用如下设置标题标题:
I have recently updated my project to react navigation 5.x. In earlier version we used to set header title as follows :
static navigationOptions = ({ navigation }) => ({
title: 'find',
});
这不适用于 React Navigation 5.x.请提出建议.
This is not working on React Navigation 5.x. Please Suggest.
答
你可以通过 2 种方法来解决;
You can do it via 2 methods;
1:将 options
设置为屏幕上的变量并保留当前代码:
1: Set options
to be a variable from your screen and keep your current code:
<Stack.Screen
name="Label"
component={Component}
options={Component.navigationOptions}
/>
// component
static navigationOptions = {
title: 'find',
};
2:通过在组件中使用 setOptions
:
2: By using setOptions
in your component:
<Stack.Screen
name="News"
component={News}
options={{
title: 'Default',
}}
/>
// component
this.props.navigation.setOptions({title: 'find'});