Use of State and setState React Native

Asked

Viewed 293 times

0

I’m trying to create the variables that will receive the User and Password data,I tried several ways using state and setState, but the way I built my application I’m not getting. Thanks for the help

import React from 'react';
import { TextInput, Text, View, TouchableOpacity } from 'react-native';
import styles from '../Style/LoginStyles'

// Tela de Login


const Login = ({ navigation }) => (


  <View style={styles.backgroundImage}>
    <View style={styles.WindowLogin}>
      <Text style={styles.Label}>Informe seus dados</Text>
      <TextInput placeholder='Login' style={styles.TextInput}/>
      <TextInput placeholder='Senha' style={styles.TextInput} secureTextEntry />
      <TouchableOpacity onPress={() => navigation.navigate('Cadastro')}>
        <View style={styles.Button}>
          <Text style={{ color: '#FFF' } , {fontSize: 24}}>Entrar</Text>
        </View>           
      </TouchableOpacity>
      <TouchableOpacity>
        <Text style={{ color: '#FFF' } , {fontSize: 18}}>Esqueci minha senha</Text>
      </TouchableOpacity>    
    </View >        
  </View >

)

Login.navigationOptions = {
  title: 'Home'
}

export default Login
  • Some mistake? There is no state in your code

1 answer

1

Explanation: You are using a stateless Component, this type of component has not been, ie state or setState will not work. You can only use props. For example, ({ navigation }), which you used, is a property. this.props.navigation, as you are using ES6 structuring, works only with navigation.

Suggestions: Exchange the stateless Component for a Component class

export default class NomeClass extends Component {

   constructor(props){
      super(props)

      this.state={
        name:"Guilherme Morgado",
        email:"[email protected]",
      }      

 }}

Or create a parent Component in a Component Class and call your stateless Component, passing the values via props.

More hints: https://itnext.io/react-component-class-vs-stateless-component-e3797c7d23ab

https://medium.com/groww-engineering/stateless-component-vs-pure-component-d2af88a1200b

Browser other questions tagged

You are not signed in. Login or sign up in order to post.