在子组件中运行父组件中的函数 - React Navigator屏幕没有redux

问题描述 投票:1回答:1

我正在使用react-native-i18n,react-native-popup-menu,react-navigation + expo(但我想尽快退出)。当用户可以选择语言(自动检测或强制:英语,德语等)和一两个其他设置时,我想要创建设置屏幕。

我正在尝试使用AsyncStorage来存储用户的偏好。当用户更改设置应用程序时,我希望在AsyncStorage和App.js中的状态对象中设置数据(如果需要,刷新主要组件/完整应用程序)。但我使用道具有问题。

App.js片段:

const navOpt = ({ navigation }) => ({   
  headerStyle: { backgroundColor: 'blue', elevation: null },
  headerRight: (<Menu>
    <MenuTrigger><MyIcon name="more" /></MenuTrigger>
    <MenuOptions>
      <MenuOption onSelect={() => navigation.navigate('Settings')} text='Settings' />
    </MenuOptions>
  </Menu>),
 });

const RootNav = StackNavigator({
  Home: { screen: HomeScreen },
  Settings: { screen: SettingsScreen }
}, {
  navigationOptions: navOpt
});


export default class App extends Component {
  constructor(props) {
    super(props);      
    this.langHandler = this.langHandler.bind(this);

    this.state = {
      isLoadingComplete: false,
      currentLang: '',
      selectedLang: null,
      selectedTheme: null
    };
   }

  async componentWillMount() {    
    try {
    AsyncStorage.multiGet(['lang', 'theme'],   (err, stores) => {
        this.setState({ selectedLang: stores[0].lang });
        this.setState({ selectedTheme: stores[0].theme });    
    });
    } catch (e) {
      console.log(e);
      return;
    }

   try {
      await I18n.initAsync();
    } catch (e) {
      console.log(e);
      return;
    }
    console.log(this.state.selectedLang);
    if (this.state.selectedLang != null && this.state.selectedLang !== '') {
        this.setState({ currentLang: this.state.selectedLang });
        } else {
        this.setState({ currentLang: I18n.locale });
        }
    this.setState({ isLoadingComplete: true });   
    console.log(this.state);
  }

  langHandler(lang) {
    console.log(`xx${lang}`);
    this.setState({
        isLoadingComplete: false
    });
}

  render() {
  if (this.state.isLoadingComplete) return (<MenuProvider><RootNav /></MenuProvider>);
    return <View />;
  }
}

SettingsScreen.js - 这是最简单的版本:

import React, { Component } from 'react';
import { StyleSheet, View, Button, TextInput, AsyncStorage } from 'react-native';

class SettingsScreen extends Component {
  static navigationOptions = {
    title: 'Settings',
  };
  constructor(props) {
    super(props);
    this.state = {
      text: ''
    };
  }
    saveData() {
      AsyncStorage.setItem('lang', this.state.text);
      this.props.langHandler(this.state.text);
    }
    render() {
      return (
        <View style={styles.container}>
          <View>
              <TextInput onChangeText={(text) => this.setState({ text })} />
             <Button title='set' onPress={() => this.saveData()} />
          </View>
        </View>
      );
    }
  }

我正在尝试设置类似的东西

 const RootNav = StackNavigator({
      Home: { screen: HomeScreen },
      Settings: { screen: props => <SettingsScreen (...props) />}

但是不行。也许我需要使用Redux?但是,如果有可能避免这种情况更好:)也许我应该以不同的方式做所有事情?

感谢您的时间。最好的祝福

react-native react-navigation
1个回答
1
投票

你在寻找的是screenProps。你必须将langHandler添加到RootNav screenProps。

render() {
  const screenProps = {
     langHandler: this.langHandler,
  };

  if (this.state.isLoadingComplete) {
    return (
       <MenuProvider>
          <RootNav screenProps={screenProps} />
      </MenuProvider>);

  }
  return <View />;
}

然后你就可以在你的langHandler中使用this.props.screenProps.langHandler访问SettingsScreen

saveData() {
  AsyncStorage.setItem('lang', this.state.text);
  this.props.screenProps.langHandler(this.state.text);
}
© www.soinside.com 2019 - 2024. All rights reserved.