Instruction file imported from BrooksPoltl/SnapConnect (
.cursor/rules/components.mdc). Copyright stays with the author.
React Native Component Best Practices
These rules ensure clean, reusable, and performant React Native components in components/ and screens/ directories.
General Guidelines
- Use functional components with arrow functions and hooks instead of class components.
- Keep components small, ideally 50-100 lines, and extract complex logic to custom hooks.
- Destructure props and state to simplify code readability.
- Name components using
PascalCase, such asUserCardorProfileScreen.
Structure
- Place reusable components in
components/and screen-specific components inscreens/. - Break down large components into smaller, focused sub-components.
- Store styles in a separate
styles.tsfile for components with extensive styling. - Example: A
UserCardcomponent should have its logic in anindex.tsxfile and styles in astyles.tsfile within aUserCard/folder.
Props and Type Safety
- Define TypeScript interfaces for props, as specified in TypeScript Rules.
- Avoid using
defaultProps; set default values in prop destructuring. - Use optional chaining for nullable props to prevent runtime errors.
- Example: A
Headercomponent should have a props interface with a requiredtitleand an optionalsubtitle, rendering the subtitle only if provided.
Performance
- Apply
React.memoto components with stable props to avoid unnecessary re-renders. - Use
FlatListfor lists, settinginitialNumToRenderandwindowSizefor efficiency. - Avoid inline functions in render; use
useCallbackfor event handlers. - Example: An
ItemListcomponent should useFlatListwith a memoized render function and limited initial rendering for performance.
Accessibility
- Add
accessibilityLabelandaccessibilityRoleto interactive elements for screen reader support. - Ensure text and backgrounds have sufficient color contrast per accessibility standards.
- Maintain a logical component hierarchy for screen reader compatibility.
- Example: A button should include an accessibility label like "Submit form" and a role of "button".
Styling
- Use
StyleSheet.createfor styles, as outlined in Styling Rules. - Avoid inline styles and reference styles from a
styles.tsfile. - Use a global theme for consistent colors and spacing, such as a primary color.
- Support responsive design using the
useWindowDimensionshook. - Example: A component should apply styles from a
styles.tsfile, using theme-defined values for padding and colors.
Reusability
- Design components with flexible props, such as a
variantprop for different styles. - Extract shared logic to hooks in
hooks/, as per Hooks Rules. - Avoid direct Firebase calls in components; fetch data in parent components or hooks.
- Example: A
Buttoncomponent should accept avariantprop to toggle between primary and secondary styles.
Error Handling
- Use error boundaries to catch rendering errors and display fallback UI.
- Show user-friendly messages for failed states, such as "Failed to load".
- Example: An error boundary component should wrap a risky component and render a fallback message if an error occurs.
Notes
- Use
SafeAreaViewfromreact-native-safe-area-contextto handle notches and status bars. - Use
expo-routerfor navigation, as recommended in General Rules. - Test components with
@testing-library/react-native, as detailed in Testing Rules. - Refer to React Native documentation for component and API guidelines.