【发布时间】:2021-01-10 13:08:44
【问题描述】:
我正在尝试使用 Axios 发布帖子,以便将人员添加到我的数据库中。我不断收到“'PersonForm'(文件名)类型上不存在属性'setState'”的错误消息。
我目前的代码:
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;
如果有人知道如何解决此问题或如何正确执行此操作,将不胜感激!
【问题讨论】:
标签: reactjs forms react-native form-submit setstate