Attributes in React Native

Asked

Viewed 359 times

0

Good afternoon, I am with a project in which I have to store a data of a Textinput and validate if it is empty or if it has any value.

I’ve tried using setState in input and calling a function that validates, however it seems to me the value of the input is not being stored (as if setState is not working).

I’m starting now with React-Native, I hope it was possible to understand my question.

Thank you in advance.

constructor(props){
    super(props);
    this.state = {
        name: '',
    }
}

<Nome /> ``` Aqui é onde o usuario digita o nome e seria armazenado na propriedade "name: "  ```

1 answer

1


Put all possible code to help us better understand the problem. Please!

The most important thing to get the value in React is to use state, but to set a value in state. You need to use this.setState(value)

Take a look at the code below and visit this link

import React, { Component } from 'react';
import { AppRegistry, Text, TextInput, View } from 'react-native';

export default class PizzaTranslator extends Component {
  constructor(props) {
    super(props);
    this.state = {text: ''};
  }

  render() {
    return (
      <View style={{padding: 10}}>
        <TextInput
          style={{height: 40}}
          placeholder="Type here to translate!"
          onChangeText={(text) => this.setState({text})}
        />
        <Text style={{padding: 10, fontSize: 42}}>
          {this.state.text.split(' ').map((word) => word && '').join(' ')}
        </Text>
      </View>
    );
  }
}

// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => PizzaTranslator);


  [1]: https://facebook.github.io/react-native/docs/handling-text-input

Browser other questions tagged

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