this.state is undefined during onPress event in react native

ImPuLsE

Hello i new in react native, my code is:

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

import Style from './styles/signin';
import Button from '../common/button';

export default class SignIn extends Component {
  constructor(props) {
    super(props);
    this.state = {
      email: '',
      password: ''
    };
  }

  render(){
    return(
      <View style={Style.container}>
        <Text style={Style.label}>Email</Text>
        <TextInput
          style={Style.input}
          onChangeText={(text) => this.setState({email: text})}
          value={this.state.email}
        />
        <Text style={Style.label}>Password</Text>
        <TextInput
          style={Style.input}
          onChangeText={(text) => this.setState({password: text})}
          value={this.state.password}
          secureTextEntry={true}
        />
        <Button text={'Sign in'} onPress={this.onPress}/>
      </View>
    );
  }
  onPress(){
    console.log(this.state.email);
  }
}

When i fill this form and press sign in, i have this error message: "Cannot read property 'email' of undefined". Thank you for the help!

jmancherje

This is a binding issue. The simplest solution will be to change your JSX for the button tag like so:

<Button text={'Sign in'} onPress={this.onPress.bind(this)} />

ES6 classes lose the autobinding you may have been used to with es5 react.createClass. You have to be more conscious of your binding when using ES6 for react components.

Another option is to bind the method in your constructor like so:

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

    this.onPress = this.onPress.bind(this)
  }

Or you could even utilize a fat arrow es6 syntax function to maintain the 'this' binding to the component you're creating:

<Button text={'Sign in'} onPress={() => this.onPress()} />

UPDATE:

To update this again, if your environment supports some ES7 features (which I believe react-native built from react-native init or create-react-native-app shoudl do) you can use this notation to auto-bind your class methods that utilize the this keyword.

// This is auto-bound so `this` is what you'd expect
onPress = () => {
    console.log(this.state.email);
};

instead of

// This is not auto-bound so `this.state` will be `undefined`
onPress(){
  console.log(this.state.email);
}

The best options are to use the ES7 feature if available or to bind in your constructor. Using an anonymous function onPress={() => this.onPress()} or onPress={this.onPress.bind(this)} directly on your Button is much less favorable for performance reasons.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React-Native ListView onPress - undefined is not an object

From Dev

React Native : how to change view on onPress event

From Dev

react-native-maps - polygon onPress event

From Dev

How to get onPress event from ScrollView Component in React Native

From Dev

How do I bind the storeId to an onPress event in React-Native?

From Dev

React this.props is undefined in state event?

From Dev

react-native: `this.state` is undefined in `render`

From Dev

React Native : State value is evaluated as undefined

From Dev

React Native Hamburger onPress Issue

From Dev

react native passing onPress with value

From Dev

React Native,not correct working onPress

From Dev

React Native / Redux - setState - Cannot update during an existing state transition

From Dev

React Native - Handle onPress event to show image while specific ListView Pressed using Timeline ListView

From Java

TypeError: undefined is not an object (evaluating 'this.state.Gas') in React Native

From Dev

TypeError: undefined is not an object(evaluating 'navigation.state')-React Native

From Dev

React-Native - undefined is not an object" (evaluating 'this.state.*) _renderRow

From Dev

React Native Undefined is not an Object (evaluating 'this.state.input')

From Dev

Why Native DOM event always reset React Component state

From Dev

Why Native DOM event always reset React Component state

From Dev

React native props,ref value is undefined inside keyboard hide event?

From Java

React Native: View onPress does not work

From Java

React Native onPress being called automatically

From Dev

React Native: onPress on component doesn't function

From Dev

React Native: TouchableOpacity onPress problems inside a ScrollView

From Dev

Render a component onPress(TouchableOpacity/Button) in React Native

From Dev

React-Native TouchableHighlit OnPress function

From Dev

TouchableHighLight onPress inside a map function in react native

From Dev

React Native navigation with onPress function in for loop

From Dev

Button change style after onPress with React Native

Related Related

  1. 1

    React-Native ListView onPress - undefined is not an object

  2. 2

    React Native : how to change view on onPress event

  3. 3

    react-native-maps - polygon onPress event

  4. 4

    How to get onPress event from ScrollView Component in React Native

  5. 5

    How do I bind the storeId to an onPress event in React-Native?

  6. 6

    React this.props is undefined in state event?

  7. 7

    react-native: `this.state` is undefined in `render`

  8. 8

    React Native : State value is evaluated as undefined

  9. 9

    React Native Hamburger onPress Issue

  10. 10

    react native passing onPress with value

  11. 11

    React Native,not correct working onPress

  12. 12

    React Native / Redux - setState - Cannot update during an existing state transition

  13. 13

    React Native - Handle onPress event to show image while specific ListView Pressed using Timeline ListView

  14. 14

    TypeError: undefined is not an object (evaluating 'this.state.Gas') in React Native

  15. 15

    TypeError: undefined is not an object(evaluating 'navigation.state')-React Native

  16. 16

    React-Native - undefined is not an object" (evaluating 'this.state.*) _renderRow

  17. 17

    React Native Undefined is not an Object (evaluating 'this.state.input')

  18. 18

    Why Native DOM event always reset React Component state

  19. 19

    Why Native DOM event always reset React Component state

  20. 20

    React native props,ref value is undefined inside keyboard hide event?

  21. 21

    React Native: View onPress does not work

  22. 22

    React Native onPress being called automatically

  23. 23

    React Native: onPress on component doesn't function

  24. 24

    React Native: TouchableOpacity onPress problems inside a ScrollView

  25. 25

    Render a component onPress(TouchableOpacity/Button) in React Native

  26. 26

    React-Native TouchableHighlit OnPress function

  27. 27

    TouchableHighLight onPress inside a map function in react native

  28. 28

    React Native navigation with onPress function in for loop

  29. 29

    Button change style after onPress with React Native

HotTag

Archive