React Native在静态NavigationOptions中使用this.props

时间:2019-10-22 06:53:39

标签: javascript reactjs react-native react-navigation react-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}

因为现在刷新应用程序时,出现以下错误:props未定义。

1 个答案:

答案 0 :(得分:0)

您可以使用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);