{"id":2183,"date":"2024-11-10T16:34:57","date_gmt":"2024-11-10T08:34:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2183\/"},"modified":"2024-11-10T16:34:57","modified_gmt":"2024-11-10T08:34:57","slug":"%e6%9e%84%e5%bb%ba%e4%b9%90%e8%a7%82%e6%9b%b4%e6%96%b0%e7%9a%84%e6%95%b0%e6%8d%ae%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2183\/","title":{"rendered":"\u6784\u5efa\u4e50\u89c2\u66f4\u65b0\u7684\u6570\u636e\u8868"},"content":{"rendered":"<h2> \u4ecb\u7ecd <\/h2>\n<p>\u4eca\u5929\uff0c\u6211\u5c06\u5206\u4eab\u5982\u4f55\u4f7f\u7528\u73b0\u4ee3 react \u6a21\u5f0f\u6784\u5efa\u4e00\u4e2a\u7cbe\u7f8e\u7684\u98df\u54c1\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002\u6211\u4eec\u5c06\u4e13\u6ce8\u4e8e\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u65e0\u7f1d\u4e50\u89c2\u66f4\u65b0\u7684\u54cd\u5e94\u5f0f\u6570\u636e\u8868\uff0c\u5c06 tanstack query\uff08\u4ee5\u524d\u79f0\u4e3a react query\uff09\u7684\u5f3a\u5927\u529f\u80fd\u4e0e mantine \u7684\u7ec4\u4ef6\u5e93\u76f8\u7ed3\u5408\u3002<\/p>\n<h2> \u9879\u76ee\u6982\u51b5 <\/h2>\n<h3> \u8981\u6c42 <\/h3>\n<ul>\n<li>\u5728\u6570\u636e\u8868\u4e2d\u663e\u793a\u98df\u54c1<\/li>\n<li>\u6dfb\u52a0\u65b0\u9879\u76ee\u5e76\u7acb\u5373\u53cd\u9988<\/li>\n<li>\u4f18\u96c5\u5730\u5904\u7406\u52a0\u8f7d\u548c\u9519\u8bef\u72b6\u6001<\/li>\n<li>\u63d0\u4f9b\u6d41\u7545\u7684\u4e50\u89c2\u66f4\u65b0<\/li>\n<\/ul>\n<h3> \u6280\u672f\u5806\u6808 <\/h3>\n<ul>\n<li> <strong>tanstack \u67e5\u8be2<\/strong>\uff1a\u670d\u52a1\u5668\u72b6\u6001\u7ba1\u7406<\/li>\n<li> <strong>mantine ui<\/strong>\uff1a\u7ec4\u4ef6\u5e93\u548c\u8868\u5355\u7ba1\u7406<\/li>\n<li> <strong>mantine react table<\/strong>\uff1a\u9ad8\u7ea7\u8868\u529f\u80fd<\/li>\n<li> <strong>wretch<\/strong>\uff1a\u5e72\u51c0\u7684 api \u8c03\u7528<\/li>\n<li> <strong><\/strong>\uff1a\u7c7b\u578b\u5b89\u5168<\/li>\n<\/ul>\n<h2> \u5b9e\u65bd\u6307\u5357 <\/h2>\n<h3> 1. \u8bbe\u7acb\u57fa\u91d1\u4f1a <\/h3>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u5b9a\u4e49\u6211\u4eec\u7684\u7c7b\u578b\u548c api \u914d\u7f6e\uff1a<\/p>\n<pre>\/\/ types\nexport type getallfoods = {\n  id: number;\n  name: string;\n  category: string;\n};\n\nexport type createnewfoodtype = pick&lt;\n  getallfoods,\n  | 'name'\n  | 'category'\n&gt;;\n\n\/\/ api configuration\nexport const api = wretch('&lt;http:\/\/localhost:9999&gt;').options({\n  credentials: 'include',\n  mode: 'cors',\n  headers: {\n    'content-type': 'application\/json',\n    accept: 'application\/json',\n  },\n});\n\n\/\/ tanstack query \nexport const getfoodoptions = () =&gt; {\n  return queryoptions({\n    querykey: ['all-foods'],\n    queryfn: async () =&gt; {\n      try {\n        return await api.get('\/foods')\n          .unauthorized(() =&gt; {\n            console.log('unauthorized');\n          })\n          .json&lt;array&lt;getallfoods&gt;&gt;();\n      } catch (e) {\n        console.log({ e });\n        throw e;\n      }\n    },\n  });\n};\n\nexport const usegetallfoods = () =&gt; {\n  return usequery({\n    ...getfoodoptions(),\n  });\n};\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 2. \u6784\u5efa\u6570\u636e\u8868 <\/h3>\n<p>\u4f7f\u7528 mantine react table \u7684\u8868\u683c\u7ec4\u4ef6\uff1a<\/p>\n<pre>const foodsview = () =&gt; {\n  const { data } = usegetallfoods();\n\n  const columns = usememo&lt;mrt_columndef&lt;getallfoods&gt;[]&gt;(\n    () =&gt; [\n      {\n        accessorkey: 'id',\n        header: 'id',\n      },\n      {\n        accessorkey: 'name',\n        header: 'name',\n      },\n      {\n        accessorkey: 'category',\n        header: 'category',\n      },\n      \/\/ ... other columns\n    ],\n    []\n  );\n\n  const table = usemantinereacttable({\n    columns,\n    data: data ?? [],\n    \/\/ optimistic update animation\n    mantinetablebodycellprops: ({ row }) =&gt; ({\n      style: row.original.id &lt; 0 ? {\n        animation: 'shimmer-and-pulse 2s infinite',\n        background: `linear-gradient(\n          110deg,\n          transparent 33%,\n          rgba(83, 109, 254, 0.2) 50%,\n          transparent 67%\n        )`,\n        backgroundsize: '200% 100%',\n        position: 'relative',\n      } : undefined,\n    }),\n  });\n\n  return &lt;mantinereacttable table={table} \/&gt;;\n};\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 3. \u521b\u5efa\u8868\u5355 <\/h3>\n<p>\u7528\u4e8e\u6dfb\u52a0\u65b0\u98df\u7269\u7684\u8868\u5355\u7ec4\u4ef6\uff1a<\/p>\n<pre>const createnewfood = () =&gt; {\n  const { mutate } = usecreatenewfood();\n\n  const forminputs = [\n    { name: 'name', type: 'text' },\n    { name: 'category', type: 'text' },\n  ];\n\n  const form = useform&lt;createnewfoodtype&gt;({\n    initialvalues: {\n      name: '',\n      category: '',\n      \/\/ ... other fields\n    },\n  });\n\n  return (\n    &lt;box mt=\"md\"&gt;\n      &lt;form onsubmit={form.onsubmit((data) =&gt; mutate(data))}&gt;\n        &lt;flex direction=\"column\" gap=\"xs\"&gt;\n          {forminputs.map((input) =&gt; (\n            &lt;textinput\n              key={input.name}\n              {...form.getinputprops(input.name)}\n              label={input.name}\n              tt=\"uppercase\"\n              type={input.type}\n            \/&gt;\n          ))}\n          &lt;button type=\"submit\" mt=\"md\"&gt;\n            create new\n          &lt;\/button&gt;\n        &lt;\/flex&gt;\n      &lt;\/form&gt;\n    &lt;\/box&gt;\n  );\n};\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 4. \u5b9e\u65bd\u4e50\u89c2\u66f4\u65b0 <\/h3>\n<p>\u6211\u4eec\u5b9e\u73b0\u7684\u6838\u5fc3 &#8211; tanstack \u67e5\u8be2\u7a81\u53d8\u4e0e\u4e50\u89c2\u66f4\u65b0\uff1a<\/p>\n<pre>export const usecreatenewfood = () =&gt; {\n  const queryclient = usequeryclient();\n\n  return usemutation({\n    mutationkey: ['create-new-food'],\n    mutationfn: async (data: createnewfoodtype) =&gt; {\n      await new promise(resolve =&gt; settimeout(resolve, 3000)); \/\/ demo delay\n      return api.url('\/foods').post(data).json&lt;getallfoods&gt;();\n    },\n    onmutate: async (newfood) =&gt; {\n      \/\/ cancel in-flight queries\n      await queryclient.cancelqueries({ querykey: ['all-foods'] });\n\n      \/\/ snapshot current state\n      const previousfoods = queryclient.getquerydata&lt;getallfoods[]&gt;(['all-foods']);\n\n      \/\/ create optimistic entry\n      const optimisticfood: getallfoods = {\n        id: -math.random(),\n        ...newfood,\n        verified: false,\n        createdby: 0,\n        createdat: new date().toisostring(),\n        updatedat: new date().toisostring(),\n      };\n\n      \/\/ update cache optimistically\n      queryclient.setquerydata(['all-foods'], (old) =&gt;\n        old ? [...old, optimisticfood] : [optimisticfood]\n      );\n\n      return { previousfoods };\n    },\n    onerror: (err, _, context) =&gt; {\n      \/\/ rollback on error\n      if (context?.previousfoods) {\n        queryclient.setquerydata(['all-foods'], context.previousfoods);\n      }\n    },\n    onsettled: () =&gt; {\n      \/\/ refetch to ensure consistency\n      queryclient.invalidatequeries({ querykey: ['all-foods'] });\n    },\n  });\n};\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 5. \u52a8\u753b\u98ce\u683c <\/h3>\n<p>\u52a8\u753b\u5c06\u6211\u4eec\u4e50\u89c2\u7684\u66f4\u65b0\u5e26\u5165\u751f\u6d3b\uff1a<\/p>\n<pre>@keyframes shimmer-and-pulse {\n  0% {\n    background-position: 200% 0;\n    transform: scale(1);\n    box-shadow: 0 0 0 0 rgba(83, 109, 254, 0.2);\n  }\n  50% {\n    background-position: -200% 0;\n    transform: scale(1.02);\n    box-shadow: 0 0 0 10px rgba(83, 109, 254, 0);\n  }\n  100% {\n    background-position: 200% 0;\n    transform: scale(1);\n    box-shadow: 0 0 0 0 rgba(83, 109, 254, 0);\n  }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u6700\u4f73\u5b9e\u8df5 <\/h2>\n<ol>\n<li> <strong>\u4e50\u89c2\u66f4\u65b0<\/strong>\n<ul>\n<li>\u7acb\u5373\u66f4\u65b0 ui\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c<\/li>\n<li>\u901a\u8fc7\u56de\u6eda\u5904\u7406\u9519\u8bef\u60c5\u51b5<\/li>\n<li>\u901a\u8fc7\u9002\u5f53\u7684\u5931\u6548\u4fdd\u6301\u6570\u636e\u4e00\u81f4\u6027<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u7c7b\u578b\u5b89\u5168<\/strong>\n<ul>\n<li>\u4f7f\u7528 typescript \u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u53ef\u7ef4\u62a4\u6027<\/li>\n<li>\u4e3a\u6570\u636e\u7ed3\u6784\u5b9a\u4e49\u6e05\u6670\u7684\u63a5\u53e3<\/li>\n<li>\u5c3d\u53ef\u80fd\u5229\u7528\u7c7b\u578b\u63a8\u65ad<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u6027\u80fd<\/strong>\n<ul>\n<li>\u66f4\u65b0\u671f\u95f4\u53d6\u6d88\u6b63\u5728\u8fdb\u884c\u7684\u67e5\u8be2<\/li>\n<li>\u4f7f\u7528\u6b63\u786e\u7684\u67e5\u8be2\u5931\u6548<\/li>\n<li>\u5b9e\u65bd\u9ad8\u6548\u7684\u8868\u5355\u72b6\u6001\u7ba1\u7406<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u7528\u6237\u4f53\u9a8c<\/strong>\n<ul>\n<li>\u63d0\u4f9b\u5373\u65f6\u53cd\u9988<\/li>\n<li>\u663e\u793a\u52a0\u8f7d\u72b6\u6001<\/li>\n<li>\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2> \u672a\u6765\u7684\u589e\u5f3a\u529f\u80fd <\/h2>\n<p>\u5728\u60a8\u7684\u5b9e\u65bd\u4e2d\u8003\u8651\u8fd9\u4e9b\u6539\u8fdb\uff1a<\/p>\n<ul>\n<li>\u64a4\u6d88\/\u91cd\u505a\u529f\u80fd<\/li>\n<li>\u8868\u5355\u9a8c\u8bc1\u89c4\u5219<\/li>\n<li>\u9519\u8bef\u8fb9\u754c\u5b9e\u73b0<\/li>\n<\/ul>\n<h2> \u7ed3\u679c <\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173068291465677.jpg\" class=\"aligncenter\" title=\"\u6784\u5efa\u4e50\u89c2\u66f4\u65b0\u7684\u6570\u636e\u8868\u63d2\u56fe\" alt=\"\u6784\u5efa\u4e50\u89c2\u66f4\u65b0\u7684\u6570\u636e\u8868\u63d2\u56fe\" \/><\/p>\n<p>\u5b8c\u6210\u8bf7\u6c42\u540e<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173068291443477.jpg\" class=\"aligncenter\" title=\"\u6784\u5efa\u4e50\u89c2\u66f4\u65b0\u7684\u6570\u636e\u8868\u63d2\u56fe1\" alt=\"\u6784\u5efa\u4e50\u89c2\u66f4\u65b0\u7684\u6570\u636e\u8868\u63d2\u56fe1\" \/><\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u6b64\u5b9e\u73b0\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u73b0\u4ee3 react \u6a21\u5f0f\u521b\u5efa\u5f3a\u5927\u7684\u6570\u636e\u7ba1\u7406\u7cfb\u7edf\u3002 tanstack query\u3001mantine ui \u548c\u6df1\u601d\u719f\u8651\u7684\u4e50\u89c2\u66f4\u65b0\u7684\u7ed3\u5408\u521b\u9020\u4e86\u6d41\u7545\u548c\u4e13\u4e1a\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u8bb0\u4f4f\uff1a<\/p>\n<ul>\n<li>\u8ba9\u4f60\u7684\u7ec4\u4ef6\u4fdd\u6301\u4e13\u6ce8\u4e14\u53ef\u7ef4\u62a4<\/li>\n<li>\u5904\u7406\u6240\u6709\u53ef\u80fd\u7684\u72b6\u6001\uff08\u52a0\u8f7d\u3001\u9519\u8bef\u3001\u6210\u529f\uff09<\/li>\n<li>\u4f7f\u7528 typescript \u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf<\/li>\n<li>\u5728\u5b9e\u65bd\u4e2d\u8003\u8651\u7528\u6237\u4f53\u9a8c<\/li>\n<\/ul>\n<hr>\n<p><em>\u60a8\u5728 react \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u65bd\u4e50\u89c2\u66f4\u65b0\u65f6\u9762\u4e34\u54ea\u4e9b\u6311\u6218\uff1f\u5728\u4e0b\u9762\u7684\u8bc4\u8bba\u4e2d\u5206\u4eab\u60a8\u7684\u7ecf\u9a8c\u3002<\/em><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6784\u5efa\u4e50\u89c2\u66f4\u65b0\u7684\u6570\u636e\u8868\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd \u4eca\u5929\uff0c\u6211\u5c06\u5206\u4eab\u5982\u4f55\u4f7f\u7528\u73b0\u4ee3 react \u6a21\u5f0f\u6784\u5efa\u4e00\u4e2a\u7cbe\u7f8e\u7684\u98df\u54c1\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002\u6211\u4eec\u5c06\u4e13\u6ce8\u4e8e\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u65e0\u7f1d\u4e50\u89c2\u66f4\u65b0\u7684\u54cd\u5e94\u5f0f\u6570\u636e\u8868\uff0c\u5c06 tanstack query\uff08\u4ee5\u524d\u79f0\u4e3a react query\uff09\u7684\u5f3a\u5927\u529f\u80fd\u4e0e mantine \u7684\u7ec4\u4ef6\u5e93\u76f8\u7ed3\u5408\u3002 \u9879\u76ee\u6982\u51b5 \u8981\u6c42 \u5728\u6570\u636e\u8868\u4e2d\u663e\u793a\u98df\u54c1 \u6dfb\u52a0\u65b0\u9879\u76ee\u5e76\u7acb\u5373\u53cd\u9988 \u4f18\u96c5\u5730\u5904\u7406\u52a0\u8f7d\u548c\u9519\u8bef\u72b6\u6001 \u63d0\u4f9b\u6d41\u7545\u7684\u4e50\u89c2\u66f4\u65b0 \u6280\u672f\u5806\u6808 tanstack \u67e5\u8be2\uff1a\u670d\u52a1\u5668\u72b6\u6001\u7ba1\u7406 mantine ui\uff1a\u7ec4\u4ef6\u5e93\u548c\u8868\u5355\u7ba1\u7406 mantine react table\uff1a\u9ad8\u7ea7\u8868\u529f\u80fd wretch\uff1a\u5e72\u51c0\u7684 api \u8c03\u7528 \uff1a\u7c7b\u578b\u5b89\u5168 \u5b9e\u65bd\u6307\u5357 1. \u8bbe\u7acb\u57fa\u91d1\u4f1a \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u5b9a\u4e49\u6211\u4eec\u7684\u7c7b\u578b\u548c api \u914d\u7f6e\uff1a \/\/ types export type getallfoods = { id: number; name: string; category: string; }; export type createnewfoodtype = pick&lt; getallfoods, | [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2183","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2183"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2183\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}