Reactjs 如何获取数据
我正在处理react项目,在我的项目中,我试图从后端获取数据。我的Api也工作得很好,因为我无法以表格格式在前端显示数据。所以请帮助我以表格格式显示数据。 这是我的密码Reactjs 如何获取数据,reactjs,Reactjs,我正在处理react项目,在我的项目中,我试图从后端获取数据。我的Api也工作得很好,因为我无法以表格格式在前端显示数据。所以请帮助我以表格格式显示数据。 这是我的密码 import React, { Component } from 'react'; import './Profiles.css'; import axios from 'axios'; export default class Profiles extends Component { constructor(prop
import React, { Component } from 'react';
import './Profiles.css';
import axios from 'axios';
export default class Profiles extends Component {
constructor(props) {
super(props)
this.state = {
products: []
}
}
async getProfiles() {
try {
const res = await axios.get('http://localhost:5000/api/products');
this.setState( { products: res.data } )
} catch (error) {
console.log(error)
}
}
render() {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<table className="table table-bordered">
<thead>
<tr>
<th>Brand</th>
<th>Price</th>
<th>Replacement</th>
<th>Discount</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{this.state.jobs.map(currentValue =>
<tr>
<td>{currentValue.name}</td>
<td>{currentValue.position}</td>
<td>{currentValue.location}</td>
<td>{currentValue.salary}</td>
<td>
<button className='btn btn-primary'>Edit</button>
</td>
<td>
<button className='btn btn-danger'>Delete</button>
</td>
</tr>
)}
</tbody>
</table>
import React,{Component}来自'React';
导入“/Profiles.css”;
从“axios”导入axios;
导出默认类配置文件扩展组件{
建造师(道具){
超级(道具)
此.state={
产品:[]
}
}
异步getProfiles(){
试一试{
const res=等待axios.get('http://localhost:5000/api/products');
this.setState({products:res.data})
}捕获(错误){
console.log(错误)
}
}
render(){
返回(
烙印
价格
替换
优惠
编辑
删除
{this.state.jobs.map(currentValue=>
{currentValue.name}
{currentValue.position}
{currentValue.location}
{currentValue.salary}
编辑
删除
)}
从“React”导入React,{Component};
导入“/Profiles.css”;
从“axios”导入axios;
导出默认类配置文件扩展组件{
建造师(道具){
超级(道具)
此.state={
职位:[]
}
}
componentDidMount(){
这个文件名为getProfiles()
}
异步getProfiles(){
试一试{
const res=等待axios.get('http://localhost:7500/api/registration');
this.setState({jobs:res.data})
}捕获(错误){
console.log(错误)
}
}
render(){
返回(
名称
位置
位置
薪水
编辑
删除
{this.state.jobs.map(currentValue=>
{currentValue.name}
{currentValue.position}
{currentValue.location}
{currentValue.salary}
编辑
删除
)}
您忘记了在componentDidMount()中调用getProfiles(),所以什么也不会发生
import React, { Component } from 'react';
import './Profiles.css';
import axios from 'axios';
export default class Profiles extends Component {
constructor(props) {
super(props)
this.state = {
jobs: []
}
}
componentDidMount() {
this.getProfiles()
}
async getProfiles() {
try {
const res = await axios.get('http://localhost:7500/api/registration');
this.setState( { jobs: res.data } )
} catch (error) {
console.log(error)
}
}
render() {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<table className="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Location</th>
<th>Salary</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{this.state.jobs.map(currentValue =>
<tr>
<td>{currentValue.name}</td>
<td>{currentValue.position}</td>
<td>{currentValue.location}</td>
<td>{currentValue.salary}</td>
<td>
<button className='btn btn-primary'>Edit</button>
</td>
<td>
<button className='btn btn-danger'>Delete</button>
</td>
</tr>
)}
</tbody>
</table>