react-native - 将 this.state 传递给静态函数
问题描述
我正在尝试将headerTitle
标题栏中的 设置为登录用户的用户名:this.state.username
。但是,当我尝试设置navigationOptions
一个函数时,我无法传入当前状态。
在 ?中设置 headerTitle 的最佳方法是static navigationOptions
什么?我相信navigationOptions
必须保持静态,否则将不会显示任何内容。
class profile extends React.Component {
static navigationOptions =
{
headerTitle: this.state.username
};
const TabStack = createBottomTabNavigator(
{
Home: {
screen: home,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="home" size={25} style={{ color: tintColor }} />
),
},
},
Explore: {
screen: explore,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="search" size={25} style={{ color: tintColor }} />
),
}
},
Profile: {
screen: profile,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="person" size={25} style={{ color: tintColor }} />
),
}
},
Create: {
screen: createCompetition,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="add" size={25} style={{ color: tintColor }} />
),
}
},
Payment: {
screen: payment,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="attach-money" size={25} style={{ color: tintColor }} />
),
title: 'Payment',
}
}
},
{
tabBarOptions: {
showIcon: true,
showLabel: false,
activeTintColor: 'black',
style: { backgroundColor: 'white', }
},
},
)
TabStack.navigationOptions = ({ navigation, screenProps }) => {
const childOptions = getActiveChildNavigationOptions(navigation, screenProps)
return {
title: childOptions.title,
headerLeft: childOptions.headerLeft,
headerRight: childOptions.headerRight,
}
}
解决方案
所以我没有使用反应导航标题我禁用它并使用我自己的cutsom,
这是我的 Header.js :
import React, {Fragment, Component} from 'react';
import {View, Text, TouchableOpacity, Image, SafeAreaView} from 'react-native';
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from 'react-native-responsive-screen';
import {headerStyles} from '../style/headerStyles';
export default class Header extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<View
style={{
flexDirection: 'row',
paddingBottom: hp('3.125%'),
justifyContent: 'space-between',
alignItems: 'center',
}}>
<View style={{opacity: 0}}>
<Image source={require('../assets/images/crossIcon.png')} />
</View>
<View style={{flexShrink: 1}}>
<Text style={headerStyles.headerText}>{this.props.title}</Text>
</View>
<View style={{left: -20, paddingLeft: 10, marginLeft: 10}}>
<TouchableOpacity
style={headerStyles.imageView}
onPress={() => this.props.navigation.goBack()}>
<Image
style={{height: 15, width: 15}}
source={require('../assets/images/crossIcon.png')}
/>
</TouchableOpacity>
</View>
</View>
);
}
}
您可以导入任何组件,例如:
import Header from '../components/Header';
return(
<Header title={this.state.userName} navigation={this.props.navigation} />
)
有了这个,你就有足够的能力用标题做任何你想做的事情。
希望能帮助到你。随时怀疑。
推荐阅读
- mysql - mySQL LOAD DATA INFILE 每隔两行丢失一次
- java - SQLite 中的 SQL 错误或缺少数据库(“:01”附近:语法错误)
- linux - 如何正确使用 sed 将长文件名中的所有空格替换为连字符
- vue.js - 如何在 vue build 上有更多的工人?
- jquery - jquery如何按键打印数组值
- ios - 使用 UIActivityViewController 保存文件后如何知道用户保存的文件名?
- spring-boot - Spring Boot中JPA Repository的Save方法的单元测试
- flutter - 多行推送通知颤动
- visual-studio-code - 在 vscode 远程禁用自动终端
- c++ - 多个未命名的嵌套类