Button - Enabled and Disabled

3

I made a form with React Native, however I want the Button to be disabled when the TextInput is empty (not filled in), and when everything is filled in, the Button is re-enabled.

How can I do this? Can you send me examples of how I do this?

Thanks

    
asked by anonymous 17.11.2016 / 03:51

1 answer

0

In the disabled property as you will use pure javascript, you can do a Boolean check. Here's an example of how to use it.

class Form extends React.Component {
  state = {
    login:'',
    senha:''
  }

  render() {
    const { login, senha } = this.state;
    return(
      <View style={{flex:1,justifyContent:'center', alignItems:'center'}}>
        <TextInput placeholder='Login' 
          value={ login } 
          onChangeText={ text => this.setState({ login:text }) }
        />
        <TextInput placeholder='Senha' 
          value={ senha } 
          onChangeText={ text => this.setState({ senha: text }) } 
        />
        <Button 
        title='logar'
          onPress={() => { this.logar()}}
          disabled={ !(login.length && senha.length) }
        />
      </View>
    )
  }
}
    
23.09.2018 / 07:02