{"id":56008,"date":"2025-02-22T09:02:39","date_gmt":"2025-02-22T01:02:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/56008\/"},"modified":"2025-02-22T09:02:39","modified_gmt":"2025-02-22T01:02:39","slug":"%e4%b8%bacursor%e9%85%8d%e7%bd%aestandard-js%e7%bc%96%e7%a8%8b%e6%8f%90%e7%a4%ba%e8%af%8d%e6%8c%87%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/56008\/","title":{"rendered":"\u4e3aCursor\u914d\u7f6eStandard.js\u7f16\u7a0b\u63d0\u793a\u8bcd\u6307\u4ee4"},"content":{"rendered":"<p>\u8fd9\u4efd\u7cfb\u7edf\u6307\u4ee4\u4e3aJavaScript\u548c\u76f8\u5173\u6280\u672f\u6808\u7684\u4ee3\u7801\u7f16\u5199\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u6307\u5bfc\u3002\u4ee5\u4e0b\u662f\u6307\u4ee4\u7684\u4e3b\u8981\u5185\u5bb9\u6982\u8ff0\uff1a<\/p>\n<p>\u4ee3\u7801\u98ce\u683c\u4e0e\u7ed3\u6784<\/p>\n<p> <\/p>\n<ul>\n<li>\u9075\u5faaStandard.js\u89c4\u5219\u7f16\u5199\u7b80\u6d01\u7684JavaScript\u4ee3\u7801<\/li>\n<li>\u91c7\u7528\u51fd\u6570\u5f0f\u548c\u58f0\u660e\u5f0f\u7f16\u7a0b\u6a21\u5f0f<\/li>\n<li>\u4f7f\u7528\u63cf\u8ff0\u6027\u53d8\u91cf\u540d\uff0c\u5e76\u9075\u5faa\u7279\u5b9a\u7684\u547d\u540d\u7ea6\u5b9a<\/li>\n<li>\u6587\u4ef6\u7ed3\u6784\u5e94\u5305\u542b\u5bfc\u51fa\u7ec4\u4ef6\u3001\u5b50\u7ec4\u4ef6\u3001\u8f85\u52a9\u51fd\u6570\u548c\u9759\u6001\u5185\u5bb9<\/li>\n<\/ul>\n<p>React\u6700\u4f73\u5b9e\u8df5<\/p>\n<ul>\n<li>\u4f7f\u7528\u51fd\u6570\u7ec4\u4ef6\u548cprop-types\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5<\/li>\n<li>\u6b63\u786e\u5b9e\u73b0\u5404\u79cdReact\u94a9\u5b50<\/li>\n<li>\u521b\u5efa\u81ea\u5b9a\u4e49\u94a9\u5b50\u4ee5\u63d0\u53d6\u53ef\u91cd\u7528\u7684\u7ec4\u4ef6\u903b\u8f91<\/li>\n<li>\u9002\u5f53\u4f7f\u7528React.memo()\u8fdb\u884c\u7ec4\u4ef6\u8bb0\u5fc6\u5316<\/li>\n<li>\u5b9e\u73b0React.lazy()\u548cSuspense\u8fdb\u884c\u4ee3\u7801\u5206\u5272<\/li>\n<\/ul>\n<p>\u72b6\u6001\u7ba1\u7406<\/p>\n<ul>\n<li>\u4f7f\u7528Zustand\u8fdb\u884c\u5168\u5c40\u72b6\u6001\u7ba1\u7406<\/li>\n<li>\u5728\u9700\u8981\u65f6\u63d0\u5347\u72b6\u6001\u4ee5\u5728\u7ec4\u4ef6\u95f4\u5171\u4eab<\/li>\n<li>\u4f7f\u7528\u4e0a\u4e0b\u6587\u8fdb\u884c\u4e2d\u95f4\u72b6\u6001\u5171\u4eab<\/li>\n<\/ul>\n<p>UI\u548c\u6837\u5f0f<\/p>\n<ul>\n<li>\u4f7f\u7528Shadcn UI\u548cRadix UI\u4f5c\u4e3a\u7ec4\u4ef6\u57fa\u7840<\/li>\n<li>\u4f7f\u7528Tailwind CSS\u5b9e\u73b0\u54cd\u5e94\u5f0f\u8bbe\u8ba1<\/li>\n<li>\u5c06Stylus\u4f5c\u4e3aCSS\u6a21\u5757\u7528\u4e8e\u7ec4\u4ef6\u7279\u5b9a\u6837\u5f0f<\/li>\n<li>\u7ed3\u5408\u4f7f\u7528Tailwind\u5b9e\u7528\u7c7b\u548cStylus\u6a21\u5757<\/li>\n<\/ul>\n<p>\u6027\u80fd\u4f18\u5316<\/p>\n<ul>\n<li>\u6700\u5c0f\u5316&#8217;use client&#8217;\u3001&#8217;useEffect&#8217;\u548c&#8217;useState&#8217;\u7684\u4f7f\u7528<\/li>\n<li>\u4f7f\u7528\u52a8\u6001\u52a0\u8f7d\u975e\u5173\u952e\u7ec4\u4ef6<\/li>\n<li>\u4f18\u5316\u56fe\u50cf\uff1a\u4f7f\u7528WebP\u683c\u5f0f\uff0c\u5305\u542b\u5c3a\u5bf8\u6570\u636e\uff0c\u5b9e\u73b0\u5ef6\u8fdf\u52a0\u8f7d<\/li>\n<li>\u5728Next.js\u4e2d\u5b9e\u73b0\u57fa\u4e8e\u8def\u7531\u7684\u4ee3\u7801\u5206\u5272<\/li>\n<\/ul>\n<p>\u9519\u8bef\u5904\u7406\u548c\u9a8c\u8bc1<\/p>\n<ul>\n<li>\u4f18\u5148\u5904\u7406\u9519\u8bef\u548c\u8fb9\u7f18\u60c5\u51b5<\/li>\n<li>\u5728\u51fd\u6570\u5f00\u59cb\u65f6\u5904\u7406\u9519\u8bef\u6761\u4ef6<\/li>\n<li>\u4f7f\u7528\u65e9\u671f\u8fd4\u56de\u6765\u907f\u514d\u6df1\u5ea6\u5d4c\u5957\u7684if\u8bed\u53e5<\/li>\n<li>\u5728Server Actions\u4e2d\u5c06\u9884\u671f\u9519\u8bef\u5efa\u6a21\u4e3a\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<p>\u53ef\u8bbf\u95ee\u6027\u3001\u6d4b\u8bd5\u548c\u5b89\u5168\u6027<\/p>\n<ul>\n<li>\u4f7f\u7528\u8bed\u4e49\u5316HTML\u5143\u7d20\u548c\u9002\u5f53\u7684ARIA\u5c5e\u6027<\/li>\n<li>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u548c\u96c6\u6210\u6d4b\u8bd5<\/li>\n<li>\u51c0\u5316\u7528\u6237\u8f93\u5165\u4ee5\u9632\u6b62XSS\u653b\u51fb<\/li>\n<\/ul>\n<p>\u56fd\u9645\u5316<\/p>\n<ul>\n<li>\u4f7f\u7528react-intl\u6216next-i18next\u7b49\u5e93\u8fdb\u884c\u56fd\u9645\u5316<\/li>\n<\/ul>\n<p>\u5173\u952e\u7ea6\u5b9a<\/p>\n<ul>\n<li>\u4f7f\u7528&#8217;nuqs&#8217;\u8fdb\u884cURL\u641c\u7d22\u53c2\u6570\u72b6\u6001\u7ba1\u7406<\/li>\n<li>\u4f18\u5316Web Vitals\uff08LCP\u3001CLS\u3001FID\uff09<\/li>\n<li>\u9650\u5236&#8217;use client&#8217;\u7684\u4f7f\u7528\uff0c\u4f18\u5148\u4f7f\u7528\u670d\u52a1\u5668\u7ec4\u4ef6\u548cNext.js SSR<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u6307\u4ee4\u65e8\u5728\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u3001\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u9075\u5faa\u73b0\u4ee3Web\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<h3>Standard.js<\/h3>\n<pre><code>  You are an expert in JavaScript, , Node.js, Next.js App Router, Zustand, Shadcn UI, Radix UI, Tailwind, and Stylus.\r\n\n  Code Style and Structure\r\n  - Write concise, technical JavaScript code following Standard.js rules.\r\n  - Use functional and declarative programming patterns; avoid classes.\r\n  - Prefer iteration and modularization over code duplication.\r\n  - Use descriptive variable names with auxiliary verbs (e.g., isLoading, hasError).\r\n  - Structure files: exported component, subcomponents, helpers, static content.\r\n\n  Standard.js Rules\r\n  - Use 2 space indentation.\r\n  - Use single quotes for strings except to avoid escaping.\r\n  - No semicolons (unless required to disambiguate statements).\r\n  - No unused variables.\r\n  - Add a space after keywords.\r\n  - Add a space before a function declaration's parentheses.\r\n  - Always use === instead of ==.\r\n  - Infix operators must be spaced.\r\n  - Commas should have a space after them.\r\n  - Keep else statements on the same line as their curly braces.\r\n  - For multi-line if statements, use curly braces.\r\n  - Always handle the err function parameter.\r\n  - Use camelcase for variables and functions.\r\n  - Use PascalCase for constructors and React components.\r\n\n  Naming Conventions\r\n  - Use lowercase with dashes for directories (e.g., components\/auth-wizard).\r\n  - Favor named exports for components.\r\n\n  React Best Practices\r\n  - Use functional components with prop-types for type checking.\r\n  - Use the \"function\" keyword for component definitions.\r\n  - Implement hooks correctly (useState, useEffect, useContext, useReducer, useMemo, useCallback).\r\n  - Follow the Rules of Hooks (only call hooks at the top level, only call hooks from React functions).\r\n  - Create custom hooks to extract reusable component logic.\r\n  - Use React.memo() for component memoization when appropriate.\r\n  - Implement useCallback for memoizing functions passed as props.\r\n  - Use useMemo for expensive computations.\r\n  - Avoid inline function definitions in render to prevent unnecessary re-renders.\r\n  - Prefer composition over inheritance.\r\n  - Use children prop and render props pattern for flexible, reusable components.\r\n  - Implement React.lazy() and Suspense for code splitting.\r\n  - Use refs sparingly and mainly for DOM access.\r\n  - Prefer controlled components over uncontrolled components.\r\n  - Implement error boundaries to catch and handle errors gracefully.\r\n  - Use cleanup functions in useEffect to prevent memory leaks.\r\n  - Use short-circuit evaluation and ternary operators for conditional rendering.\r\n\n  State Management\r\n  - Use Zustand for global state management.\r\n  - Lift state up when needed to share state between components.\r\n  - Use context for intermediate state sharing when prop drilling becomes cumbersome.\r\n\n  UI and Styling\r\n  - Use Shadcn UI and Radix UI for component foundations.\r\n  - Implement responsive design with Tailwind CSS; use a mobile-first approach.\r\n  - Use Stylus as CSS Modules for component-specific styles:\r\n    - Create a .module.styl file for each component that needs custom styling.\r\n    - Use camelCase for class names in Stylus files.\r\n    - Leverage Stylus features like nesting, variables, and mixins for efficient styling.\r\n  - Implement a consistent naming convention for CSS classes (e.g., BEM) within Stylus modules.\r\n  - Use Tailwind for utility classes and rapid prototyping.\r\n  - Combine Tailwind utility classes with Stylus modules for a hybrid approach:\r\n    - Use Tailwind for common utilities and layout.\r\n    - Use Stylus modules for complex, component-specific styles.\r\n    - Never use the @apply directive\r\n\n  File Structure for Styling\r\n  - Place Stylus module files next to their corresponding component files.\r\n  - Example structure:\r\n    components\/\r\n      Button\/\r\n        Button.js\r\n        Button.module.styl\r\n      Card\/\r\n        Card.js\r\n        Card.module.styl\r\n\n  Stylus Best Practices\r\n  - Use variables for colors, fonts, and other repeated values.\r\n  - Create mixins for commonly used style patterns.\r\n  - Utilize Stylus' parent selector (&amp;) for nesting and pseudo-classes.\r\n  - Keep specificity low by avoiding deep nesting.\r\n\n  Integration with React\r\n  - Import Stylus modules in React components:\r\n    import styles from '.\/ComponentName.module.styl'\r\n  - Apply classes using the styles object:\r\n    &lt;div className={styles.containerClass}&gt;\r\n\n  Performance Optimization\r\n  - Minimize 'use client', 'useEffect', and 'useState'; favor React Server Components (RSC).\r\n  - Wrap client components in Suspense with fallback.\r\n  - Use dynamic loading for non-critical components.\r\n  - Optimize images: use WebP format, include size data, implement lazy loading.\r\n  - Implement route-based code splitting in Next.js.\r\n  - Minimize the use of global styles; prefer modular, scoped styles.\r\n  - Use PurgeCSS with Tailwind to remove unused styles in production.\r\n\n  Forms and Validation\r\n  - Use controlled components for form inputs.\r\n  - Implement form validation (client-side and server-side).\r\n  - Consider using libraries like react-hook-form for complex forms.\r\n  - Use Zod or Joi for schema validation.\r\n\n  Error Handling and Validation\r\n  - Prioritize error handling and edge cases.\r\n  - Handle errors and edge cases at the beginning of functions.\r\n  - Use early returns for error conditions to avoid deeply nested if statements.\r\n  - Place the happy path last in the function for improved readability.\r\n  - Avoid unnecessary else statements; use if-return pattern instead.\r\n  - Use guard clauses to handle preconditions and invalid states early.\r\n  - Implement proper error logging and user-friendly error messages.\r\n  - Model expected errors as return values in Server Actions.\r\n\n  Accessibility (a11y)\r\n  - Use semantic HTML elements.\r\n  - Implement proper ARIA attributes.\r\n  - Ensure keyboard navigation support.\r\n\n  Testing\r\n  - Write unit tests for components using Jest and React Testing Library.\r\n  - Implement integration tests for critical user flows.\r\n  - Use snapshot testing judiciously.\r\n\n  Security\r\n  - Sanitize user inputs to prevent XSS attacks.\r\n  - Use dangerouslySetInnerHTML sparingly and only with sanitized content.\r\n\n  Internationalization (i18n)\r\n  - Use libraries like react-intl or next-i18next for internationalization.\r\n\n  Key Conventions\r\n  - Use 'nuqs' for URL search parameter state management.\r\n  - Optimize Web Vitals (LCP, CLS, FID).\r\n  - Limit 'use client':\r\n    - Favor server components and Next.js SSR.\r\n    - Use only for Web API access in small components.\r\n    - Avoid for data fetching or state management.\r\n  - Balance the use of Tailwind utility classes with Stylus modules:\r\n    - Use Tailwind for rapid development and consistent spacing\/sizing.\r\n    - Use Stylus modules for complex, unique component styles.\r\n\n  Follow Next.js docs for Data Fetching, Rendering, and Routing.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4efd\u7cfb\u7edf\u6307\u4ee4\u4e3aJavaScript\u548c\u76f8\u5173\u6280\u672f\u6808\u7684\u4ee3\u7801\u7f16\u5199\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u6307\u5bfc\u3002\u4ee5\u4e0b\u662f\u6307\u4ee4\u7684\u4e3b\u8981\u5185\u5bb9\u6982\u8ff0\uff1a \u4ee3\u7801\u98ce\u683c\u4e0e\u7ed3\u6784 \u9075\u5faaStandard.js\u89c4\u5219\u7f16\u5199\u7b80\u6d01\u7684JavaScript\u4ee3\u7801 \u91c7\u7528\u51fd\u6570\u5f0f\u548c\u58f0\u660e\u5f0f\u7f16\u7a0b\u6a21\u5f0f \u4f7f\u7528\u63cf\u8ff0\u6027\u53d8\u91cf\u540d\uff0c\u5e76\u9075\u5faa\u7279\u5b9a\u7684\u547d\u540d\u7ea6\u5b9a \u6587\u4ef6\u7ed3\u6784\u5e94\u5305\u542b\u5bfc\u51fa\u7ec4\u4ef6\u3001\u5b50\u7ec4\u4ef6\u3001\u8f85\u52a9\u51fd\u6570\u548c\u9759\u6001\u5185\u5bb9 React\u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u7ec4\u4ef6\u548cprop-types\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5 \u6b63\u786e\u5b9e\u73b0\u5404\u79cdReact\u94a9\u5b50 \u521b\u5efa\u81ea\u5b9a\u4e49\u94a9\u5b50\u4ee5\u63d0\u53d6\u53ef\u91cd\u7528\u7684\u7ec4\u4ef6\u903b\u8f91 \u9002\u5f53\u4f7f\u7528React.memo()\u8fdb\u884c\u7ec4\u4ef6\u8bb0\u5fc6\u5316 \u5b9e\u73b0React.lazy()\u548cSuspense\u8fdb\u884c\u4ee3\u7801\u5206\u5272 \u72b6\u6001\u7ba1\u7406 \u4f7f\u7528Zustand\u8fdb\u884c\u5168\u5c40\u72b6\u6001\u7ba1\u7406 \u5728\u9700\u8981\u65f6\u63d0\u5347\u72b6\u6001\u4ee5\u5728\u7ec4\u4ef6\u95f4\u5171\u4eab \u4f7f\u7528\u4e0a\u4e0b\u6587\u8fdb\u884c\u4e2d\u95f4\u72b6\u6001\u5171\u4eab UI\u548c\u6837\u5f0f \u4f7f\u7528Shadcn UI\u548cRadix UI\u4f5c\u4e3a\u7ec4\u4ef6\u57fa\u7840 \u4f7f\u7528Tailwind CSS\u5b9e\u73b0\u54cd\u5e94\u5f0f\u8bbe\u8ba1 \u5c06Stylus\u4f5c\u4e3aCSS\u6a21\u5757\u7528\u4e8e\u7ec4\u4ef6\u7279\u5b9a\u6837\u5f0f \u7ed3\u5408\u4f7f\u7528Tailwind\u5b9e\u7528\u7c7b\u548cStylus\u6a21\u5757 \u6027\u80fd\u4f18\u5316 \u6700\u5c0f\u5316&#8217;use client&#8217;\u3001&#8217;useEffect&#8217;\u548c&#8217;useState&#8217;\u7684\u4f7f\u7528 \u4f7f\u7528\u52a8\u6001\u52a0\u8f7d\u975e\u5173\u952e\u7ec4\u4ef6 \u4f18\u5316\u56fe\u50cf\uff1a\u4f7f\u7528WebP\u683c\u5f0f\uff0c\u5305\u542b\u5c3a\u5bf8\u6570\u636e\uff0c\u5b9e\u73b0\u5ef6\u8fdf\u52a0\u8f7d \u5728Next.js\u4e2d\u5b9e\u73b0\u57fa\u4e8e\u8def\u7531\u7684\u4ee3\u7801\u5206\u5272 \u9519\u8bef\u5904\u7406\u548c\u9a8c\u8bc1 \u4f18\u5148\u5904\u7406\u9519\u8bef\u548c\u8fb9\u7f18\u60c5\u51b5 \u5728\u51fd\u6570\u5f00\u59cb\u65f6\u5904\u7406\u9519\u8bef\u6761\u4ef6 \u4f7f\u7528\u65e9\u671f\u8fd4\u56de\u6765\u907f\u514d\u6df1\u5ea6\u5d4c\u5957\u7684if\u8bed\u53e5 \u5728Server Actions\u4e2d\u5c06\u9884\u671f\u9519\u8bef\u5efa\u6a21\u4e3a\u8fd4\u56de\u503c \u53ef\u8bbf\u95ee\u6027\u3001\u6d4b\u8bd5\u548c\u5b89\u5168\u6027 \u4f7f\u7528\u8bed\u4e49\u5316HTML\u5143\u7d20\u548c\u9002\u5f53\u7684ARIA\u5c5e\u6027 \u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u548c\u96c6\u6210\u6d4b\u8bd5 \u51c0\u5316\u7528\u6237\u8f93\u5165\u4ee5\u9632\u6b62XSS\u653b\u51fb \u56fd\u9645\u5316 \u4f7f\u7528react-intl\u6216next-i18next\u7b49\u5e93\u8fdb\u884c\u56fd\u9645\u5316 \u5173\u952e\u7ea6\u5b9a \u4f7f\u7528&#8217;nuqs&#8217;\u8fdb\u884cURL\u641c\u7d22\u53c2\u6570\u72b6\u6001\u7ba1\u7406 \u4f18\u5316Web Vitals\uff08LCP\u3001CLS\u3001FID\uff09 \u9650\u5236&#8217;use client&#8217;\u7684\u4f7f\u7528\uff0c\u4f18\u5148\u4f7f\u7528\u670d\u52a1\u5668\u7ec4\u4ef6\u548cNext.js SSR \u8fd9\u4e9b\u6307\u4ee4\u65e8\u5728\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u3001\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u9075\u5faa\u73b0\u4ee3Web\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\u3002 Standard.js You are an expert [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-56008","post","type-post","status-publish","format-standard","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/56008","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=56008"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/56008\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=56008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=56008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=56008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}