Variable value change in React-Native

Asked

Viewed 1,390 times

0

I am new to React-Native so I believe this is a very basic question. As I step the value of a variableinserir a descrição da imagem aqui for my JSX template? I want when I call the Somag1 function it assigns a value to Contg1 and that value changes in the JSX component.

  • Could you please put your code to facilitate assist in the response?

  • Philip is what is in the image above, not appearing ?

  • We usually do this with state: https://facebook.github.io/react-native/docs/state

  • @Lucasdecarvalho thanks man, but I think I still don’t understand.

1 answer

2


It is necessary to create an element in the state in its component. Following example:

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


class App extends Component {
  constructor() {
    super();
    this.state = {
        contG1: 0
    };
  }

  somaG1() {
    this.setState({contG1:this.state.contG1+1})
  }

  render() {
    return (
      <View>
        <Text>Valor contG1: {this.state.contG1}</Text>
        <Button onPress={() => this.somaG1()} title="Clique para somar" />
      </View>
    );
  }
}

export default App;

You can also see working online.

The state has the role of controlling the state of that component, in this case the App component of your application. It is important to remember that it is immutable, being changed with the function setState.

For more information about the state, you can access documentation.

  • Man, I haven’t made it yet 'cause he keeps making a mistake

  • 1

    Says the constructor function does not exist.

  • @Fabriciocarneiro realize that I’ve changed the way I’m creating the component. You were making a stateless component (stateless Component), as we need a component that has been, we need a Component class. For more information, you can read this post

  • 1

    I think I got it bro, thanks so much!

Browser other questions tagged

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