10 分钟上手
npm i @iostore/store @iostore/solid solid-js最小可复制示例
Section titled “最小可复制示例”import { For, Show } from 'solid-js';import { useQuery } from '@iostore/solid';
type User = { id: string; name: string };
export function UserList(props: { tenantId: string }) { const users = useQuery<User[]>({ key: ['users', props.tenantId], queryFn: ({ signal }) => fetch(`/api/users?tenant=${props.tenantId}`, { signal }).then((r) => r.json()), cancelOnDispose: true, });
return ( <Show when={!users.isLoading()} fallback={<p>Loading...</p>}> <For each={users.data() ?? []}>{(user) => <li>{user.name}</li>}</For> </Show> );}状态分支渲染建议
Section titled “状态分支渲染建议”- 首屏加载:使用
isLoading(或等价状态)。 - 后台刷新:使用
isFetching(或观察者fetchStatus)。 - 错误态:直接展示可诊断信息,不要吞掉
error。
取消语义(AbortSignal)
Section titled “取消语义(AbortSignal)”queryFn必须透传signal到fetch/ 请求客户端。- 把取消当作控制流,不当作业务失败弹窗。
- 页面快速切换场景启用取消(
cancelOnUnmount/cancelOnDispose/cancelOnUnsubscribe)。
复制后需要改的 3 处
Section titled “复制后需要改的 3 处”- 把示例 key 改成业务域 key(例如
users->orders)。 - 把请求 URL 与返回类型改成项目真实接口。
- 把重试、取消策略调成你的页面交互强度。