javascript - React Native 在静态 navigationOptions 中使用 this.props
问题描述
那是我的主题组件:
/* Imports */
import React from 'react';
import { View, Text, FlatList, TouchableOpacity } from 'react-native';
import { withTheme} from '@app/theme/themeProvider';
import { AntDesign } from '@app/utils/Icons';
import { custom } from '@app/styles/config';
import { styles } from '@app/theme/theme';
/* /Imports/ */
class ChangeTheme extends React.Component {
/* Navigation Options Like (Header, Title, Menu, Icon, Style) */
static navigationOptions = ({navigation}) => ({
title: "Промяна на темата",
headerStyle: { backgroundColor: styles(this.props).backgroundColor},
headerTitleStyle: { color: '#F5F5F5' },
headerLeft: <AntDesign name="arrowleft" size={24} color="#F5F5F5" onPress={() => {navigation.navigate('Settings')}} style={custom.headerLeft} />
});
/* /Navigation Options Like (Header, Title, Menu, Icon, Style)/ */
/* Render Method - Is Place Where You Can View All Content Of The Page */
render() {
const { themes } = this.props;
const theme = styles(this.props);
return (
<FlatList style={[custom.settingsThemeContainer, theme.backgroundColorTheme]} data={themes} renderItem={this._renderItem.bind(this)} ListHeaderComponent={this._ListHeaderComponent.bind(this)}/>
);
}
/* /Render Method - Is Place Where You Can View All Content Of The Page/ */
}
export default withTheme(ChangeTheme);
我如何设置 headerStyle : headerStyle: { backgroundColor: styles(this.props).backgroundColor}
因为当我现在刷新我的应用程序时,我收到以下错误:道具未定义。
解决方案
您可以使用 navigation.setParams 函数修复
/* Imports */
import React from 'react';
import {View, Text, FlatList, TouchableOpacity} from 'react-native';
import {withTheme} from '@app/theme/themeProvider';
import {AntDesign} from '@app/utils/Icons';
import {custom} from '@app/styles/config';
import {styles} from '@app/theme/theme';
/* /Imports/ */
class ChangeTheme extends React.Component {
/* Navigation Options Like (Header, Title, Menu, Icon, Style) */
static navigationOptions = ({navigation}) => {
return {
title: 'Промяна на темата',
headerStyle: {
backgroundColor:
navigation.state.params && navigation.state.params.backgroundColor
? navigation.state.params.backgroundColor
: '#FFF',
},
headerTitleStyle: {color: '#F5F5F5'},
headerLeft: (
<AntDesign
name="arrowleft"
size={24}
color="#F5F5F5"
onPress={() => {
navigation.navigate('Settings');
}}
style={custom.headerLeft}
/>
),
};
};
/* /Navigation Options Like (Header, Title, Menu, Icon, Style)/ */
componentDidMount() {
const {navigation} = this.props;
const {backgroundColor} = styles(this.props);
navigation.setParams({backgroundColor});
}
componentDidUpdate(prevProps) {
const {navigation} = this.props;
const {backgroundColor} = styles(this.props);
if(prevProps.navigation.state && prevProps.navigation.state.params && prevProps.navigation.state.params.backgroundColor !== backgroundColor){
navigation.setParams({backgroundColor});
}
}
/* Render Method - Is Place Where You Can View All Content Of The Page */
render() {
const {themes} = this.props;
const theme = styles(this.props);
return (
<FlatList
style={[custom.settingsThemeContainer, theme.backgroundColorTheme]}
data={themes}
renderItem={this._renderItem.bind(this)}
ListHeaderComponent={this._ListHeaderComponent.bind(this)}
/>
);
}
/* /Render Method - Is Place Where You Can View All Content Of The Page/ */
}
export default withTheme(ChangeTheme);
推荐阅读
- php - 显示来自 wp 插件表单提交的自定义错误消息
- python - OSX Finder中的“随机排序”将kMDItemFinderComment设置为文件夹中每个文件的随机哈希?
- javascript - firebase/firestore 中的时间戳
- python - 将嵌套字典转换为元组 python
- javascript - 带有监视任务的 Lerna - lerna run --parallel
- github - 更改 Github 页面上的标题
- javascript - 当我在浏览器中输入 console.log 时,为什么谷歌浏览器没有显示结果?
- sql - BigQuery 中的 CREATE OR REPLACE 是否有助于先前分区表的配额?
- python-logging - 是否可以使用字符串而不是 int 指定日志级别?
- python - 永久修改python路径