跳到主要内容

客户端排序

这里有一个根据 orderBy 字段排序的 API。通过把我们的 Collection 包裹在一个进行排序的 Query 中,可以确保在推入新帖子之后依然保持正确的顺序。

示例代码一开始按 title 排序。试着添加一些帖子,看看它们是否按正确的排序顺序插入。

import { useSuspense } from '@data-client/react';
import { getPosts } from './getPosts';
import NewPost from './NewPost';

export default function PostList({ author }: Props) {
  const posts = useSuspense(getPosts, {
    author,
    orderBy: 'title',
    group: 'react',
  });
  return (
    <div>
      {posts.map(post => (
        <div key={post.pk()}>{post.title}</div>
      ))}
      <NewPost author={author} />
    </div>
  );
}
interface Props {
  author: string;
}
结果
Store▶