import axios from 'axios';
class MyComponent extends React.Component { constructor(props) { super(props); this.state = { data: [] // 初始化为空数组 }; }
componentDidMount() { axios.get('/api/data') .then(response => { this.setState({ data: response.data // 将返回的数据更新到状态中 }) }) .catch(error => { console.log(error); }); }
render() { return (
{item.name}
))}