Theme Best Practices
Given below is a detailed checklist of best practices for Fynd Partners to follow while building a theme for Fynd Commerce using React:
Fynd Best Practices
-
FPI (Fynd Platform Interface) Functions
- Write SSR-compatible code
- Assume all the code will be executed in the server
- Careful with
window
anddocument
objects, always check if the context is browser before using them - Have the
fpi
data fetch calls in two places.- One for SSR case: when the page is loaded first time
- One for SPA case: when the page is loaded via UI navigation
- Image Optimization
- Images should be loaded based on the screen size
Use the util fn here
transformImage
(Uses pixelbin resize).
- Images should be loaded based on the screen size
Use the util fn here
- GraphQL: Get ALL the needed data in a single call and ONLY the ones needed
- Be careful while using the
fpi
events to avoid messing up the analytics related to it. - Keep the SEO of the page in mind
- Avoid adding hacks to the system, it will come back to hurt you later
- Follow the common JS/React Debugging practices before reaching out
- NEVER use hardcoded URLs of storefront. Use action objects to ensure all the UTM parameters are retained.
- Write SSR-compatible code
-
FDK-CLI (Fynd Developer Kit Command-Line Interface):
- Install FDK-CLI globally using npm.
npm install -g @gofynd/fdk-cli
- Keep the FDL CLI updated to the latest version.
- Check logs using the
--debug
flag to resolve issues, before reaching out to us. - Use FDK-CLI commands to generate or deploy the theme.
- Validate the theme locally before deployment.
- Install FDK-CLI globally using npm.
React Best Practices
-
Component Design
- Functional Components: Prefer functional components over class components.
- Reusability: Break down UI into reusable, smaller components to avoid duplication and increase readability (ideally < 300 lines).
- Folder Structure: Organize files logically, e.g., components/, pages/, hooks/, context/.
-
State Management
- Use Hooks: Utilize useState, useReducer, and useContext for managing state locally.
- Avoid Prop Drilling: Use Context API or state libraries like Redux, Zustand, or Recoil when state needs to be shared deeply.
-
Styling
- CSS-in-JS: Use libraries like styled-components or emotion for scoped styles.
- Modular CSS: If using CSS files, prefer CSS Modules for scoping styles (.module.css).
-
Code Splitting
- Use React’s lazy and suspense for dynamic imports to reduce initial load time.
- Split large components or libraries using tools like Webpack or Vite for bundling.
-
Error Boundaries: Wrap components in error boundaries to gracefully handle rendering errors.
-
Accessibility
- Use semantic HTML tags (<button>, <form>, etc.).
- Include ARIA attributes where necessary (aria-label, aria-hidden).
- Test keyboard navigation and screen reader support.
- Use tabIndex judiciously.
- Ensure sufficient contrast between text and background (minimum contrast ratio: 4:1)
JavaScript Best Practices
-
Code Quality
- Linting: Use ESLint with a React-specific configuration.
- Formatting: Use Prettier for consistent code formatting.
- Type Safety: Use TypeScript or PropTypes for type checking.
-
Common Pitfalls
- Avoid Mutating State: Always return a new state object in state updates (setState).
- Optimize Loops and Maps: Use functional methods like .map(), .filter() for array operations.
- Avoid Anonymous Functions in JSX: Avoid inline functions in JSX for performance reasons; use useCallback.
-
Modern Syntax: Use ES6+ features like arrow functions, destructuring, let and const over var, template literals, and spread/rest operators.
-
Async Code
- Use async/await for handling promises.
- Handle errors gracefully with try/catch.
-
Performance
- Debounce/Throttle: Limit expensive operations (e.g., search inputs).
- Memoization: Use React.memo, useMemo, and useCallback to avoid unnecessary re-renders.
- Keys in Lists: Always use a unique key for list elements.
General Best Practices
-
Version Control
- Follow branching strategies like Git Flow.
- Write descriptive commit messages.
-
Testing
- Test theme configurations (e.g., settings, customizations) thoroughly.
- Use React Testing Library or Enzyme for unit testing components.
- Aim for at least 80% test coverage.
- Write snapshot tests for consistent UI.
- Use Jest for overall JavaScript testing.
- Use tools like Cypress or Playwright to test the entire flow.
-
Documentation
- Comment your code effectively where necessary.
- Use tools like Storybook for documenting components.
- Use JSDoc for utility functions and APIs.
-
Performance Monitoring
- Monitor performance with React DevTools.
- Profile your app and optimize for render times.
- Optimize images, minify code, and leverage browser caching to improve page load times.
-
Security
- Sanitize inputs to avoid XSS.
- Avoid hardcoding sensitive data; use environment variables.
- Regularly audit dependencies for vulnerabilities.
- Use environment variables for sensitive data like API keys