DevAS
DevAS

Reputation: 827

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.

ScreenShots about an issue: Login Screen

Input Component

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;

Upvotes: 0

Views: 116

Answers (1)

devserkan
devserkan

Reputation: 17608

You are missing the onChangeText prop in your custom Input component, so add it.

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

Upvotes: 2

Related Questions