【发布时间】:2020-06-29 23:33:43
【问题描述】:
我正在使用材料表构建一个表。下面是如何构建表的示例:https://material-table.com/#/docs/features/filtering
我正在尝试在data 中传递一个数组,以填充我的Userlist 数组中的字段。
我的 componentDidMount() 函数中有这个
pnp.sp.web.lists.getByTitle("Team").items.get().then((items: any[]) => {
//console.log(items);
const UserList = items.map((item) => {
return {
UsersName: item.Title,
UserEmail: item.Email_x0020_Address,
UserStatus: item.Status,
UserLocation: item.Location,
UserAdditional: item.Additional,
}
});
//console.log(UserList);
this.setState({ UserList: [...UserList] });
});
在我的渲染中我有这个:
<MaterialTable
title=""
columns={[
{ title: 'Name', field: 'UsersName' },
{ title: 'Email', field: 'UserEmail' },
{ title: 'Status', field: 'UserStatus' },
{ title: 'Location', field: 'UserLocation' },
{ title: 'Additional', field: 'UserAdditional' },
]}
data={new Promise((resolve,reject) => {
(this.state.UserList)
})}
options={{
filtering: true
}}
/>
最初我有data={this.state.Userlist},但收到_this.props.data is not a function at MaterialTable 错误。
所以我改变了,所以我在承诺中传递了数据以绕过错误,但现在收到此错误:
error TS2322: Type 'Promise<{}>' is not assignable to type '({ UsersName: any; } & { UserEmail: any; } & { UserStatus: any; } & { UserLocation: any; } & { UserAdditional: any; })[] | ((query: Query<{ UsersName: any; } & { UserEmail: any; } & { UserStatus: any; } & { UserLocation: any; } & { ...; }>) => Promise<...>)'.
【问题讨论】:
-
你希望 Promise 做什么?我对此有点困惑。根据the docs 看来
data应该是一个数组或返回数据的函数。我猜一开始你的数组不见了? -
可以像
data={this.state.data || []}一样简单,或者将您的初始状态默认为空数组?但我只能用有限的信息猜测 -
@Brian Thompson 所以当我将其更改为
data={this.state.UserList || []}时,我得到`TypeError: _this.props.data is not a function at MaterialTable.` 错误 -
我也做了一个
{JSON.stringify(this.state.UserList)},所有元素都在数组中。
标签: javascript arrays reactjs typescript promise