| name | documentation |
| description | WHAT: JSDoc and inline documentation standards for React components and hooks. WHEN: documenting components, writing JSDoc for functions, explaining complex logic. KEYWORDS: jsdoc, documentation, comments, @description, @context, @example, inline, props, hooks, api. |
Documentation Standards
Best practices for writing clear and maintainable code documentation.
When to Use
Use these standards when:
- Documenting React components and their props
- Writing JSDoc for complex functions or hooks
- Adding inline comments to explain non-obvious logic
- Creating README files for features or modules
- Documenting API interfaces and type definitions
- Explaining "why" decisions were made
Core Principles
Document the "Why", Not the "What"
Code should be self-documenting for what it does. Comments should explain why it exists or why it's implemented in a particular way.
✅ Good:
setIsSubmitting(true);
if (isSubscribeDisabled) {
return 'disabled';
}
prefetchNextPage();
❌ Bad:
setIsLoading(true);
fetchData();
counter++;
Why: "Why" comments provide valuable context that code alone cannot convey, helping future developers understand intent and reasoning.
Use JSDoc for Public APIs
Document all components, hooks, and functions that are exported or part of a public API.
✅ Good:
export const transformReactivationPriceResponse = (
response: ReactivationPriceResponse
): VoucherPriceInfo => {
};
Why: JSDoc provides structured documentation that appears in IDE tooltips and generates documentation automatically.
Component Documentation
Document with @description, @context, and @example
Use these three tags to provide comprehensive component documentation.
✅ Good:
export const NavigationEntryProvider = ({
children,
linking,
requiredRepositories,
repositoryLoadingFallback,
}: NavigationEntryProviderProps) => {
};
Structure:
- @description - Explains what the component does and its purpose
- @context - Describes when and where to use it
- @example - Shows concrete usage with realistic code
Document Props Interfaces
Add JSDoc comments to each prop explaining its purpose.
✅ Good:
export interface RecipeCardProps {
recipe: Recipe;
onPress?: (recipeId: string) => void;
onAddToCart?: (recipe: Recipe) => void;
isLoading?: boolean;
testID?: string;
}
Why: Property documentation clarifies purpose and usage, appears in IDE tooltips, and reduces need to look at implementation.
Hook Documentation
Document custom hooks with their usage patterns and return values.
✅ Good:
export const useGetExternalRecipesInfinite = (
params: GetExternalRecipesParams,
options?: UseInfiniteQueryOptions
) => {
};
Why: Hooks have complex return values that benefit from documentation showing typical usage patterns.
Type Documentation
Document Interfaces and Types
Add descriptions to complex interfaces and their properties.
✅ Good:
export interface ReactivationBannerConfig {
subscriptionId: string;
deeplinkVoucherCode: string;
dcId: string;
shouldTriggerReactivationWebView: boolean;
}
Why: Interface documentation explains the purpose of each property and provides examples where helpful.
Inline Comments
Comment Complex Logic
Add comments to explain non-obvious logic or calculations.
✅ Good:
const discountPercent = Math.ceil(((originalPrice - discountedPrice) / originalPrice) * 100);
setIsAnimating(true);
Why: Complex calculations and non-obvious logic benefit from explanation of the approach and reasoning.
TODO and FIXME Comments
Use structured comments for tracking future work.
✅ Good:
Format:
TODO - Future improvement or feature
TODO(name) - Assigned to specific person
FIXME - Known bug that needs fixing
HACK - Temporary solution needing proper fix
Why: Structured TODO comments make it easy to search for and track technical debt.
README Files
Feature Documentation
Create README files for complex features explaining architecture and usage.
Structure:
# Feature Name
## Overview
Brief description of what the feature does
## Architecture
- `components/` - UI components
- `hooks/` - Custom hooks
- `stores/` - State management
- `constants.ts` - Feature constants
## Usage
```tsx
Code example showing how to use the feature
Analytics Events
List of analytics events tracked by this feature
Testing
Instructions for running tests
**Why:** README files provide high-level feature documentation and onboarding for new developers.
## Common JSDoc Tags
### Standard Tags
```typescript
/**
* Brief one-line description of the function.
*
* Longer multi-line description providing more context about
* what the function does and when to use it.
*
* @param paramName - Description of parameter
* @returns Description of return value
* @throws {ErrorType} Description of when error is thrown
* @example Example code showing usage
* @see RelatedFunction for related functionality
* @deprecated Use newFunction() instead
* @default Default value if applicable
*/
@description Tag
Provides comprehensive explanation of component or function behavior.
@context Tag
Explains when and where to use the component or function.
Anti-Patterns
Don't State the Obvious
❌ Bad:
counter++;
setIsLoading(true);
fetchUserProfile();
Don't Let Comments Become Outdated
❌ Bad:
fetchProductDetails(productId);
Solution: Remove or update comments when code changes.
Don't Over-Document
❌ Bad:
const getName = (user: User) => user.name;
Why: Simple, self-explanatory code doesn't need JSDoc.
Best Practices
- Document the Why - Explain reasoning and intent, not implementation
- Use JSDoc for APIs - Document all exported functions, hooks, and components
- Provide Examples - Show concrete usage in @example tags
- Keep Current - Update documentation when code changes
- Be Concise - Clear and brief is better than verbose
- Use @context - Help developers understand when to use code
- Document Edge Cases - Explain non-obvious behavior or limitations
Additional Resources
For implementation examples and patterns, see: