Undefined is not an object evaluating this.state. *

I had a problem sending data to the express REST API in which I use fetchin my reaction-based application. Here is my code:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';
import Button from 'react-native-button';
import DeviceInfo from 'react-native-device-info'

export default class ReliefMobile extends Component {
  state: any;

  constructor(props) {
    super(props);
    this.state = {
      currentLocation: {latitude: 40.6391, longitude: 10.9969},
      name: 'Some dumb name',
      description: 'Some dumb description',
      deviceId: DeviceInfo.getUniqueID()
    }
  }

  addData() {
    fetch('http://localhost:3000/api/effort/add', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        name: this.state.name,
        description: this.state.description,
        deviceId: this.state.deviceId,
        currentLocation: this.state.currentLocation
      })
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native
        </Text>
        <Text style={styles.instructions}>
          Your device ID is {DeviceInfo.getUniqueID()}
        </Text>
        <Text style={styles.instructions}>
        Effort Name: {this.state.name}
        </Text>
        <Text style={styles.instructions}>
        Effort Description: {this.state.description}
        </Text>
        <Text style={styles.instructions}>
        Location: {this.state.currentLocation.latitude}, {this.state.currentLocation.longitude}
        </Text>
        <Button onPress={this.addData}>Add data</Button>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

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

When I try to push my button to call a function addData, I get this error: undefined is not an object (evaluating this.state.name).

When loading the application, my state variables seem to load very well in the area <Text/>:

Imgur

But when I post it, it shows: Imgur

When I change the body fetchto something like

body: JSON.stringify({name: 'some name', description: 'some description'})

. , this fetch, addData() - let that = this; that.state.name, etc, .

+3
3

, . onClick - : onClick={this.addData.bind(this)}. , .

+14

/, .

<Button onPress={this.addData.bind(this)}>Add data</Button>

https://facebook.imtqy.com/react/docs/handling-events.html

// This syntax ensures `this` is bound within handleClick
return (
  <button onClick={(e) => this.handleClick(e)}>
    Click me
  </button>
);
+6

You must bind a non-rendering function in your constructor. In your constructor, just add:

this.addData = this.addDate.bind(this);

You can also use ES6 as another alternative:

addData = () => { ... }

This will be described here: https://babeljs.io/blog/2015/06/07/react-on-es6-plus in the arrow function section.

+2
source

Source: https://habr.com/ru/post/1660001/


All Articles