Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change state when Button pressed?

Tags:

react-native

I'm new to React Native and unfamiliar with js.

I want the program to show what I wrote in TextInput when I pressed the Button (there's a Text below the Button). I figured maybe I should make two state: put state1 text as Text input, and put state2 mimin as TextInput input, and when Button pressed, put state2 mimin to state1 text.

I've tried with the code below but it gave me Red Page when I click the Button.

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

export default class Hella extends Component {

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

render() {
   return (
       <View style={styles.container}>

        <TextInput
          style={{height: 40}}
          placeholder="Type here to translate!"
          onChangeText={(mimin) => this.setState({mimin})}
        />
        <Button
          onPress={onButtonPress}
          title="Learn More"
          color="#841584"
          accessibilityLabel="Learn more about this purple button"
        />
        <Text style={styles.instructions}>
          {this.state.text}
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F5FCFF',
  }
});

const onButtonPress = () => {
  Hella.setState({
      text: Hella.state.mimin  -------> where the error happened
    });
};

AppRegistry.registerComponent('Hella', () => Hella);

The error was

undefined is not an object (evaluating 'Hella.state.mimin')

onButtonPress
<project location>/index.android.js:61

What did I do wrong? How should I declare it? Where can I learn more?

like image 576
Konayuki Avatar asked Nov 10 '16 06:11

Konayuki


People also ask

How do I change state after click?

We have to set initial state value inside constructor function and set click event handler of the element upon which click, results in changing state. Then pass the function to the click handler and change the state of the component inside the function using setState.

How do I change my state value in React?

To change a value in the state object, use the this. setState() method. When a value in the state object changes, the component will re-render, meaning that the output will change according to the new value(s).

How do I change my state in native React?

The most common way to set state in React Native is by using React's setState() method. We also have the Context API to avoid prop drilling and pass the state down many levels without passing it to individual children in the tree.

How do you change your state to initial state?

To reset a component to its initial state:Store the initial state in a variable. When an event occurs, call the setState() function, passing it the initial state.


2 Answers

Your onButtonPress should be inside class since you want to do setState

export default class Hella extends Component {
  constructor(props) {
    ...
  }

  onButtonPress = () => {
    this.setState({
      text: this.state.mimin
    });
  }

  render() {
    return (
      ...
      <Button
        onPress={this.onButtonPress}
        ...
      />
      ...
    );
  }
}

React Native uses a lot of React concepts. Learning basics of React will help you a lot https://facebook.github.io/react/tutorial/tutorial.html

like image 124
vinayr Avatar answered Oct 05 '22 06:10

vinayr


The function definition should be like below.

onButtonPress = () => {
    this.setState({
      text: this.state.mimin
    });
}
like image 40
Saikiran Kyatham Avatar answered Oct 05 '22 04:10

Saikiran Kyatham