我正在使用物料表建立桌子。这是表格的构建示例: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<...>)'.
答案 0 :(得分:1)
我基本上不了解ReactJS,我是Angular的人,但是在搜索时发现了这一点。
执行流程:
方法1:
将方法从componentWillMount更改为UNSAFE_componentWillMount,因为componentWillMount已于2018年弃用。更多信息https://dev.to/torianne02/componentwillmount-vs-componentdidmount-5f0n
UNSAFE_componentWillMount()
{
this.setState({ UserList: [] });
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] });
});
}
方法2: 在构造函数中初始化状态:
constructor() {
// Required step: always call the parent class' constructor
super(props);
// Set the state directly.
this.state = {
UserList: [],
//if you have any other state variables add here
}
}
在componentDidMount中进行get调用
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] });
})
}
两种情况下的渲染功能
render(){
return ( <MaterialTable
title=""
columns={[
{ title: 'Name', field: 'UsersName' },
{ title: 'Email', field: 'UserEmail' },
{ title: 'Status', field: 'UserStatus' },
{ title: 'Location', field: 'UserLocation' },
{ title: 'Additional', field: 'UserAdditional' },
]}
data={this.state.Userlist}
options={{
filtering: true
}}
/>)
}