【发布时间】:2020-11-14 10:53:27
【问题描述】:
我是 react 新手,我想将对象数组分配给 React.FC 状态。 我的 api 逻辑如下所示:
class LandApi {
async getLands(): Promise<Land[]> {
const response = await fetch('http://localhost:8080/api/lands', {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
return response.json();
}
}
我有这个功能组件:
const MapLands: React.FC = () =>
{
let [state, setState] = useState([])
useEffect(() => {
LandApi.getLands().then((data) => {
setState({
landList: data //the problem is here
})
})
})
return (
<div>
{
//Map logic here
}
</div>
)
}
如何将 Land[] 数组“放入”功能组件状态,以便稍后映射它?
正如您所知,我对网络编程很陌生。
衷心感谢,
塞缪尔
【问题讨论】:
标签: reactjs typescript promise react-hooks state