Reactjs React表呈现未定义的数据
我目前正在处理一个React表,该表将由RESTAPI调用填充。以下是输出:Reactjs React表呈现未定义的数据,reactjs,react-hooks,Reactjs,React Hooks,我目前正在处理一个React表,该表将由RESTAPI调用填充。以下是输出: [{"zone":"Central","location":"Atlantic Superstore","address":"650 Portland St, Dartmouth","date":"05/13/2021","exposure"
[{"zone":"Central","location":"Atlantic Superstore","address":"650 Portland St, Dartmouth","date":"05/13/2021","exposure":"09:00 - 11:30","advice":"Get tested immediately"},{"zone":"Central","location":"Sobeys Mumford","address":"6990 Mumford Road, Halifax","date":"31/05/2021","exposure":"13:00 - 14:30","advice":"Get tested Immediately"}]
我使用以下代码来调用它:
export function GetContacts() {
const [loadingData, setLoadingdata] = useState(true);
const [contacts, setContacts] = useState([]);
useEffect(() => {
fetch(
`https://mz6gux8sca.execute-api.us-east-1.amazonaws.com/dev/contact`,
{
method: 'GET'
}
)
.then(res => res.json())
.then(response => {
setContacts(response);
setLoadingdata(false);
})
.catch(error => console.log(error));
}, [])
console.log("contacts: ", contacts)
const columns = React.useMemo(
() => [
{
Header: "Zone",
accessor: "zoneID",
},
{
Header: "Location",
accessor: "contactLocation",
},
{
Header: "Address",
accessor: "address",
},
{
Header: "Date",
accessor: "date",
},
{
Header: "Window",
accessor: "exposureWindow",
},
{
Header: "Advice",
accessor: "advice",
},
],
[],
);
这是可行的,在控制台中,我得到了一个正确填充的联系人列表。但是,在我添加以下内容后:
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({ columns, contacts }, useSortBy)
抛出错误,联系人的控制台输出为空。错误为
TypeError:无法读取未定义的属性“forEach”
——大概是因为联系人是空的。如果我移除那个块,一切都会恢复正常。我在这里不知所措,因此任何洞察都将不胜感激。我认为您以错误的方式传递钩子选项。React表需要data
选项,因此useTable({columns,data:contacts})
应该可以工作