今天,我将分享如何使用现代 react 模式构建一个精美的食品数据库管理系统。我们将专注于创建一个具有无缝乐观更新的响应式数据表,将 tanstack query(以前称为 react query)的强大功能与 mantine 的组件库相结合。
首先,让我们定义我们的类型和 api 配置:
// types
export type getallfoods = {
id: number;
name: string;
category: string;
};
export type createnewfoodtype = pick<
getallfoods,
| 'name'
| 'category'
>;
// api configuration
export const api = wretch('<http://localhost:9999>').options({
credentials: 'include',
mode: 'cors',
headers: {
'content-type': 'application/json',
accept: 'application/json',
},
});
// tanstack query
export const getfoodoptions = () => {
return queryoptions({
querykey: ['all-foods'],
queryfn: async () => {
try {
return await api.get('/foods')
.unauthorized(() => {
console.log('unauthorized');
})
.json<array<getallfoods>>();
} catch (e) {
console.log({ e });
throw e;
}
},
});
};
export const usegetallfoods = () => {
return usequery({
...getfoodoptions(),
});
};
使用 mantine react table 的表格组件:
const foodsview = () => {
const { data } = usegetallfoods();
const columns = usememo<mrt_columndef<getallfoods>[]>(
() => [
{
accessorkey: 'id',
header: 'id',
},
{
accessorkey: 'name',
header: 'name',
},
{
accessorkey: 'category',
header: 'category',
},
// ... other columns
],
[]
);
const table = usemantinereacttable({
columns,
data: data ?? [],
// optimistic update animation
mantinetablebodycellprops: ({ row }) => ({
style: row.original.id < 0 ? {
animation: 'shimmer-and-pulse 2s infinite',
background: `linear-gradient(
110deg,
transparent 33%,
rgba(83, 109, 254, 0.2) 50%,
transparent 67%
)`,
backgroundsize: '200% 100%',
position: 'relative',
} : undefined,
}),
});
return <mantinereacttable table={table} />;
};
用于添加新食物的表单组件:
const createnewfood = () => {
const { mutate } = usecreatenewfood();
const forminputs = [
{ name: 'name', type: 'text' },
{ name: 'category', type: 'text' },
];
const form = useform<createnewfoodtype>({
initialvalues: {
name: '',
category: '',
// ... other fields
},
});
return (
<box mt="md">
<form onsubmit={form.onsubmit((data) => mutate(data))}>
<flex direction="column" gap="xs">
{forminputs.map((input) => (
<textinput
key={input.name}
{...form.getinputprops(input.name)}
label={input.name}
tt="uppercase"
type={input.type}
/>
))}
<button type="submit" mt="md">
create new
</button>
</flex>
</form>
</box>
);
};
我们实现的核心 - tanstack 查询突变与乐观更新:
export const usecreatenewfood = () => {
const queryclient = usequeryclient();
return usemutation({
mutationkey: ['create-new-food'],
mutationfn: async (data: createnewfoodtype) => {
await new promise(resolve => settimeout(resolve, 3000)); // demo delay
return api.url('/foods').post(data).json<getallfoods>();
},
onmutate: async (newfood) => {
// cancel in-flight queries
await queryclient.cancelqueries({ querykey: ['all-foods'] });
// snapshot current state
const previousfoods = queryclient.getquerydata<getallfoods[]>(['all-foods']);
// create optimistic entry
const optimisticfood: getallfoods = {
id: -math.random(),
...newfood,
verified: false,
createdby: 0,
createdat: new date().toisostring(),
updatedat: new date().toisostring(),
};
// update cache optimistically
queryclient.setquerydata(['all-foods'], (old) =>
old ? [...old, optimisticfood] : [optimisticfood]
);
return { previousfoods };
},
onerror: (err, _, context) => {
// rollback on error
if (context?.previousfoods) {
queryclient.setquerydata(['all-foods'], context.previousfoods);
}
},
onsettled: () => {
// refetch to ensure consistency
queryclient.invalidatequeries({ querykey: ['all-foods'] });
},
});
};
动画将我们乐观的更新带入生活:
@keyframes shimmer-and-pulse {
0% {
background-position: 200% 0;
transform: scale(1);
box-shadow: 0 0 0 0 rgba(83, 109, 254, 0.2);
}
50% {
background-position: -200% 0;
transform: scale(1.02);
box-shadow: 0 0 0 10px rgba(83, 109, 254, 0);
}
100% {
background-position: 200% 0;
transform: scale(1);
box-shadow: 0 0 0 0 rgba(83, 109, 254, 0);
}
}
在您的实施中考虑这些改进:

完成请求后

此实现演示了如何使用现代 react 模式创建强大的数据管理系统。 tanstack query、mantine ui 和深思熟虑的乐观更新的结合创造了流畅和专业的用户体验。
记住:
您在 react 应用程序中实施乐观更新时面临哪些挑战?在下面的评论中分享您的经验。
以上就是构建乐观更新的数据表的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号