【发布时间】:2021-04-22 18:21:20
【问题描述】:
我在 React JS 中有我的应用程序。我正在从 http://localhost:8080/api/suppliers/supplier/list 的 API 获取数据:
这是我从 Google Chrome 控制台中的 REST API 获得的数据:
0:{
id: 4
supplierFirstname: "Tom"
supplierLastName: "ABC"
supplierTitle: "TomTheSupplier"
accountNumber: 1122234444
address: "111 ADrive, 1234 ST."
companyName: "TheTom Company & Associates"
email: "tomtomjayjay@email.com"
hourlyRate: 29
phoneNumber: 123456789
otherPhoneNumber: 1023456789
paymentTerms: "Credit"
notes: "Some Supplier"
createdAt: null
typeOfGoods: "Supplies"
website: "www.abc_123.com"
products: [{…}]
components:
[
0: {id: 5, name: "AComponent", unit: null, quantity: 0, componentCost: 0, …}
]
}
这是我的 React 代码:
class SupplierData extends React.Component {
constructor(props) {
super(props);
this.state = {
supplier: [
{
id: 0,
supplierTitle: "Supplier Title",
supplierFirstName: "First Name",
supplierLastName: "Last Name",
companyName: "Company Name",
phoneNumber: "Phone Number",
otherPhoneNumber: "Phone Number (Other)",
accountNumber: "Account Number",
email: "Email",
address: "Address",
website: "Website",
hourlyRate: "Hourly Rate",
typeOfGoods: "Type Of Goods",
paymentTerms: "Payment Terms",
createdAt: "Created At",
notes: "Notes",
products: "Products",
components: "Components",
},
],
errorMessage: [],
};
}
listAllSuppliers = async () => {
return await axios
.get(`http://localhost:8080/api/suppliers/supplier/list`)
.then((response) => {
let apiResults = response.data;
console.log(apiResults);
this.setState({ supplier: apiResults }); <--- The error happens here.
})
.catch((error) => {
this.setState({ errorMessage: this.state.errorMessage.push(error) });
});
};
componentDidMount() {
this.ListAllSuppliers();
}
}
export default SupplierData;
我面临的问题是反应状态。我收到以下错误:
Warning: Can't call setState on a component that is not yet mounted. This is a no-op, but it might
indicate a bug in your application. Instead, assign to `this.state` directly or define a `state =
{};` class property with the desired state in the SupplierData component.
问题 1: (非常重要) 我要设置状态。
有什么办法可以解决上述错误?
问题 2: 这是在以下文件中调用上述类的正确方法吗? 我有以下代码:
import SupplierData from './..';
export const A = (props) =>{
const {id, supplierTitle, supplierFirstName, supplierLastName,
companyName, phoneNumber, otherPhoneNumber, accountNumber, email,
address, website, hourlyRate, typeOfGoods, paymentTerms,
createdAt, notes, products, components, status } = props;
let mySupplier = new SupplierData();
let listData = mySupplier.listAllSuppliers();
return(
<tr>
<td>
<Card.Link as={Link} to={Routes.Suppliers.path} className="fw-normal">
{SupplierData.map((t, id) => <tr key={id}> {t.fetchedData} </tr>)} <--- Here is where the error is
</Card.Link>
</td>
</tr>
);
}
}
【问题讨论】:
标签: javascript node.js reactjs axios