如何在React Native中从任何其他函数调用静态函数(static navigationOptions
)?
使用this
关键字时失败,但是可以通过调用再次渲染static navigationOptions
吗?
答案 0 :(得分:0)
如果要动态更改导航选项,请尝试这样
static navigationOptions = ({ navigation }) => {
return {
title: navigation.getParam('otherParam', 'A Nested Details Screen'),
};
};
this.props.navigation.setParams({otherParam: 'Updated!'})
另一种方法
static navigationOptions = ({ navigation }) => {
const {state} = navigation;
return {
title: `${state.params.title}`,
};
};
ChangeThisTitle = (titleText) => {
const {setParams} = this.props.navigation;
setParams({ title: titleText })
}
随时随地致电this.ChangeThisTitle('your title')
答案 1 :(得分:0)
他们唯一实现此目标的方法是使用导航参数。因此,请使用setParams设置headerleft属性标志或值。那将解决问题。下面提到的代码应在您的课程中使用。
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
let buttonView =
<TouchableOpacity style={navItemStyle} activeOpacity={0.7} onPress={() => { params.logoutClick() }}>
<Text style={navItemTxt}> Logout</Text>
</TouchableOpacity >
return {
title: 'Home',
headerLeft: params.showHeaderLeft && buttonView
};
};
componentDidMount() {
this.props.navigation.setParams({
showHeaderLeft: this.props.headerFlag,
//headerFlag used above is your value and showHeaderLeft is the name of the param
});
}