React Native 'setState' 不存在?

React Native 'setState' does not exist?

我正在尝试使用 Axios 创建一个 post,这样我就可以将一个人添加到我的数据库中。我一直收到错误消息“属性 'setState' 在类型 'PersonForm' (fileName) 上不存在”。

我目前的代码:

import React from 'react';
import { View, TextInput, Text, Button } from 'react-native';
import { PersonForCreate } from '../data/person';

import axios from "axios";

class PersonForm extends React.Component {
state = {
    patients: [],
    firstName: "",
    lastName: "",
    nin: "",
  };

  constructor(props) {
    super(props);
    this.addPatient = this.addPatient.bind(this);
  }

  addPatient() {
    let patientAdd = {
      firstName: this.state.firstName,
      lastName: this.state.lastName,
      nin: this.state.nin,
    };

    axios.post(`http://127.0.0.1:8000/person`, patientAdd).then((res) => {
      console.log(res);
      console.log(res.data);
    });
  }

render() {
    return (
        <View>
            <TextInput 
                key="firstName"
                placeholder="First Namee"
                onChange={(e) => {
                    this.setState({ firstName: e.target.value });
                  }}            
            />
            <TextInput 
                key="lastName"
                placeholder="Last Name"
                onChange={(e) => {
                    this.setState({ lastName: e.target.value });
                  }}              
            />  
            <TextInput 
                key="nin"
                placeholder="National Insurance Number"
                onChange={(e) => {
                    this.setState({ nin: e.target.value });
                  }}              
            />      
            <Button title="Submit" onPress={this.addPatient}></Button>                          
        </View>
    )
}
}

export default PersonForm;

如果有人知道如何解决此问题或如何正确执行此操作,将不胜感激!

状态应该在构造函数中,将其更改为:

  constructor(props) {
    super(props);
    this.addPatient = this.addPatient.bind(this);

   this.state = {
    patients: [],
    firstName: "",
    lastName: "",
    nin: "",
  };
 }