0

这是我的主屏幕

        class HomeScreen extends React.Component {
        constructor(props) {
        super(props);
        this.state = {username: null, password: null, isPasswordHidden: true, toggleText: 'Show'};
      }

      handleToggle = () => {
        const { isPasswordHidden } = this.state;

        if (isPasswordHidden) {
          this.setState({isPasswordHidden: false});
          this.setState({toggleText: 'Hide'});
        } else {
          this.setState({isPasswordHidden: true});
          this.setState({toggleText: 'Show'});
        }
      }

      //Validate() to check whether the input username is in Mail format
      validate = (inputValue) => {
        let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ; // Regex for Emails
        // let reg = /^(\+\d{1,3}[- ]?)?\d{10}$/; // Regex for phone numbers
        return reg.test(inputValue);
      }
      clearText(fieldName) {
        this.refs[fieldName].clear(0);
      }

      render() {
        return (
          <View style={styles.container}>
            <Text style={styles.welcome}></Text>


            <TextInput
              ref={'input1'}
              style={styles.input}
              placeholder="Username"
              onChangeText={value => this.setState({username: value})}

            />

            <TextInput
              ref={'input2'}
              style={styles.input}
              placeholder="Password"
              maxLength={10}
              secureTextEntry={this.state.isPasswordHidden}
              onChangeText={value => this.setState({password: value})}

            />

            <TouchableOpacity
              onPress={this.handleToggle}
            >
              <Text>{this.state.toggleText}</Text>

            </TouchableOpacity>

            <View style={{padding: 20}}>
              <TouchableOpacity onPress={() => {

                if (!this.validate(this.state.username)) {
                  Alert.alert("Invalid");
                  Keyboard.dismiss();
                } else if (this.state.username === 'vinay@gmail.com' && this.state.password === 'password') {

在这里,我已将参数传递到下一个屏幕,我想在其中显示输入 textInput 字段的值。但它没有显示在下一个屏幕中。我不知道为什么。当我添加一个没有任何抽屉导航的普通堆栈导航器时,它工作正常。

                  this.props.navigation.navigate('Welcome', {
                    u_name: this.state.username,
                    p_word: this.state.password,
                  });
                  Keyboard.dismiss();
                  this.clearText('input1');
                  this.clearText('input2');
                } else if (this.state.username === null && this.state.password === null) {
                  Alert.alert("Invalid");
                } else {
                  Alert.alert("Login Failed");
                  this.clearText('input1');
                  this.clearText('input2');
                  Keyboard.dismiss();
                }

              }}>
                <View style={styles.button}>
                  <Text style={styles.buttonText}>LOGIN</Text>
                </View>
              </TouchableOpacity>
            </View>

          </View>
        );
      }
    }

这是我的欢迎屏幕

在这里,我从上一个屏幕中获取了值并存储到一个变量中。现在它正在打印 USERNAME: name 和 PASSWORD: word

 class WelcomeScreen extends Component {
    render() {
    const { navigation } = this.props;
    const u_name = navigation.getParam('username', 'name');
    const p_word = navigation.getParam('password', 'word');
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>WELCOME</Text>
        <Text>USERNAME: {JSON.stringify(u_name)}</Text>
        <Text>PASSWORD: {JSON.stringify(p_word)}</Text>
      </View>
    );
  }
}

导航器

const WelcomeTabNavigator = createBottomTabNavigator({
  Welcome: {screen: WelcomeScreen},
  Profile,
  Settings,
}, 
{
  navigationOptions:({navigation}) => {
    const {routeName} = navigation.state.routes[navigation.state.index];
    return {
      headerTitle: routeName
    };
  }
})

const WelcomeStackNavigator = createStackNavigator({
  WelcomeTabNavigator: WelcomeTabNavigator
},
{
  defaultNavigationOptions:({navigation}) => {
    return {
      headerLeft: (
        <Icon 
          style={{paddingLeft: 20}}
          onPress={() => navigation.openDrawer()}
          name="md-menu" 
          size={30}
        />
      )
    };
  }
})

const AppDrawerNavigator = createDrawerNavigator({
  Welcome: {screen: WelcomeStackNavigator}
},
{
  contentComponent:(props) => (
    <View style={{flex:1}}>
        <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
            <DrawerItems {...props} />
            <Button 
              title="Logout" 
              onPress={() => {

                props.navigation.navigate('Home')
              }}
            />
        </SafeAreaView>
    </View>
  ),
  drawerOpenRoute: 'DrawerOpen',
  drawerCloseRoute: 'DrawerClose',
  drawerToggleRoute: 'DrawerToggle'
})

const AppSwitchNavigator = createSwitchNavigator({
  Home: {screen: HomeScreen},
  Welcome: {screen: AppDrawerNavigator}
});

const AppContainer = createAppContainer(AppSwitchNavigator);
4

2 回答 2

0

I got it. It's the issue with the navigator section. I changed this

   const AppSwitchNavigator = createSwitchNavigator({
  Home: {screen: HomeScreen},
  Welcome: {screen: AppDrawerNavigator}
});

to this

  const AppSwitchNavigator = createStackNavigator({
  Home: {screen: HomeScreen},
  Welcome: {screen: AppDrawerNavigator}
});

When you add switch navigator it resets the previous screen thereby losing the input value.

于 2019-01-17T12:18:48.810 回答
0

我认为您应该在 getParam中使用用户名的u_name insted

你的代码 const u_name = navigation.getParam('username', 'name');

新代码 const u_name = navigation.getParam('u_name', 'name');

密码也是一样

或者您可以使用这样的导航器方法而不更改 getParam 方法

this.props.navigation.navigate('Welcome', { username: this.state.username, password: this.state.password, });

于 2019-01-17T10:37:34.780 回答