简体   繁体   English

状态未在React Native中更新吗?

[英]State isn't Updated in react native?

I'm developing in react native from last month and I have done a project for practice, I have some issues with it, when I Login and setState to update my state it's not being updated with sample username and password for testing. 我正在从上个月开始使用React Native开发,并且已经完成了一个实践项目,当我登录并使用setState更新状态时,它并没有使用示例用户名和密码进行更新,因此存在一些问题。

ScreenShots about an issue: ScreenShots关于一个问题: 登入画面

输入组件

This is my Code: 这是我的代码:

import React, { Component } from "react";
import { View, StyleSheet, Text, TextInput } from "react-native";

import Button from "./common/Button";
import Card from "./common/Card";
import CardItem from "./common/CardItem";
import Input from "./common/Input";

class Login extends Component {
      constructor(props) {
      super(props);
      this.state = { username: "anas", password: "123" };
      }

      handleLogin = () => {
           console.log(
                   `Email is: ${this.state.username} and pass is: ${this.state.password} .`
            );
      };
      render() {
           return (
             <View>
                <Card>
                   <CardItem>
                       <Input
                          label="Email: "
                          placeholder="Enter your Email.."
                          secureTextEntry={false}
                          onChangeText={username => this.setState({ 
                          username})}
                        />
                 </CardItem>
                 <CardItem>
                      <Input
                       label="Password: "
                       placeholder="Enter your password.."
                       secureTextEntry={true}
                       onChangeText={password => this.setState({ password})}
                      />
                 </CardItem>
                 <CardItem>
                    <Button btnTitle="Login" onPressHandle={this.handleLogin} 
                    />
                 </CardItem>
             <Text>Email: {this.state.username}</Text>
             <Text>pass: {this.state.password}</Text>
          </Card>
      </View>
    );
   }
 }
 export default Login;

You are missing the onChangeText prop in your custom Input component, so add it. 您在自定义Input组件中缺少onChangeText ,因此添加它。

<TextInput
    ...other props
    onChangeText={props.onChangeText}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM