躍然一笑
2021-06-02 18:02:14
我正在嘗試在 react-table 組件中呈現(xiàn)一些數(shù)據(jù),但是數(shù)據(jù)沒有加載。我已經(jīng)用完全相同格式的虛擬數(shù)據(jù)進行了測試,它工作正常。但是,當我進行 API 調(diào)用并獲取相同格式的數(shù)據(jù)并將其推送到我傳遞給 react-table 的數(shù)據(jù)列表時,該表不會呈現(xiàn)它。請幫我找出問題所在。干杯設置列: columns: [ { Header: "Employee ID", accessor: "EmployeeID" }, { Header: "First Name", accessor: "FirstName" }, { Header: "Last Name", accessor: "LastName" }, { Header: "Date of Birth", accessor: "DateOfBirth", }, { Header: "Status", accessor: "Status", }, { Header: "Gender", accessor: "Gender", }, { Header: "UpdatedDateUTC", accessor: "UpdatedDateUTC", } ]數(shù)據(jù)是什么樣的:{"EmployeeID":"63c571b3-bff0-4ce1-94f7-255c235580fa","FirstName":"Clive","LastName":"Thomas","Status":"ACTIVE","DateOfBirth":"/Date(697248000000+0000)/","Gender":"M","UpdatedDateUTC":"/Date(1533706298000+0000)/"}我的 API 調(diào)用以及我如何將數(shù)據(jù)項保存到狀態(tài)。(我控制臺記錄了我獲得的數(shù)據(jù)的值,并且格式正確)fetch('http://localhost:3100/employees') .then((resp) => { return resp.json() }) .then((data) => { let temp = this.state.posts; temp.push(data.Employees[1]) this.setState({posts: temp}) console.log(this.state.posts) }) .catch((error) => { console.log(error, "catch the hoop") })我還打印了成功插入列表的虛擬數(shù)據(jù)以及未插入的 API 數(shù)據(jù)。如您所見,它們顯然是相同的:
2 回答

桃花長相依
TA貢獻1860條經(jīng)驗 獲得超8個贊
不確定這是否會解決您的問題,但 IMO 您應該將其重構為
fetch('http://localhost:3100/employees')
.then((resp) => {
return resp.json()
})
.then((data) => {
let temp = [...this.state.posts]
temp.push(data.Employees[1])
this.setState({
posts: temp,
data: data
})
console.log(this.state.posts) //this will not have the newest values yet, setState is async
})
.catch((error) => {
console.log(error, "catch the hoop")
})
對反應狀態(tài)執(zhí)行操作不是一個好習慣
添加回答
舉報
0/150
提交
取消