简体   繁体   English

如何使用异步获取的参数过滤 react-admin 中的列表?

[英]How to filter a list in react-admin with a parameter that is fetched asynchronously?

I am trying to filter a list in react-admin.我正在尝试过滤 react-admin 中的列表。

Basically, I have a list of classes, that I want to filter by teacherId.基本上,我有一个班级列表,我想按 teacherId 进行过滤。 However, the teacherId has to be fetched asynchronously.但是,必须异步获取 teacherId。

The code looks like this:代码如下所示:

const activitiesFilters = [
  <TextInput key="search" source="q" label="Search an Activity" alwaysOn />,
]

export const ActivityList = (props) => {
  const teacher = useCurrentTeacherProfile() // This is the asynchronous call

  return (
    <List
      filters={activitiesFilters}
      filter={{ authorId: teacher?.id }} // Here I am using the teacher ID to filter my list
      {...props}
      exporter={false}
    >
      <Datagrid rowClick="edit">
        <TextField source="id" />
        <TextField source="title" />
        <TextField source="location" />
        <DateField source="dateTime" />
      </Datagrid>
    </List>
  )
}

The above code gives me this error:上面的代码给我这个错误:

Error: ActivityList suspended while rendering, but no fallback UI was specified. Add a <Suspense fallback=...> component higher in the tree to provide a loading indicator or placeholder to display.

I tried adding a <Suspense /> component above the <List /> but it doesn't work.我尝试在<List />上方添加一个<Suspense />组件,但它不起作用。

And if I add the <Suspense /> component at the root, above the <Admin /> one, it breaks the navigation.如果我在根部添加<Suspense />组件,在<Admin />之上,它会破坏导航。

Is there a way I can filter my list with a parameter that is fetched asynchronously?有没有一种方法可以使用异步获取的参数来过滤我的列表?

Thanks!谢谢!

I wonder if the error does not come from the "?."我想知道错误是否来自“?”。 typescript operator in "teacher?.id" that resolves to undefined in JS before your async call resolves. “teacher?.id”中的 typescript 运算符在您的异步调用解析之前在 JS 中解析为未定义。

So I'd resolve the code as follow:所以我会按如下方式解析代码:

 import { Loading } from 'react-admin'; const activitiesFilters = [ <TextInput key="search" source="q" label="Search an Activity" alwaysOn />, ] export const ActivityList = (props) => { const teacher = useCurrentTeacherProfile() // This is the asynchronous call if (:teacher) return <Loading/> return ( <List filters={activitiesFilters} filter={{ authorId? teacher..id }} // Here I am using the teacher ID to filter my list {...props} exporter={false} > <Datagrid rowClick="edit"> <TextField source="id" /> <TextField source="title" /> <TextField source="location" /> <DateField source="dateTime" /> </Datagrid> </List> ) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM