React Native Boilerplate

website·Indexed 29 days ago

https://thecodingmachine.github.io/react-native-boilerplate/

A light, simple, and scalable starter kit for React Native development. It features a pre-configured theme system with the useTheme hook, generated styles for fonts, backgrounds, borders, and gutters, and a set of helper components including ErrorBoundary, Skeleton, SafeScreen, and variant-based asset/icon renderers. The boilerplate integrates React Navigation for app structure and Reactotron for debugging.

Tokens
9.8K
Snippets
55
Records
83
Agent score
74%

What's inside React Native Boilerplate

  1. Understand the React Native Boilerplate project folder structure

    The boilerplate architecture is organized to ensure separation of concerns. The following top-level folders in src/ are used for specific application concerns:

    • src/components: Application components, organized using atomic design.
    • src/hooks: Custom hooks used across the application.
    • src/navigation: Navigator components for handling app navigation.
    • src/screens: Screen components representing the various app views.
    • src/services: Data fetching and related service logic.
    • src/theme: Application theme configuration.
    • src/translations: Language support and internationalization configuration.
  2. React Native Boilerplate core features

    The React Native Boilerplate is a lightweight, scalable template designed for a clear separation of concerns. Key built-in features include:

    • Language Flexibility: Support for both JavaScript and TypeScript.
    • Navigation: Integrated with React Navigation for app routing and structure.
    • Data Fetching: Powered by TanStack Query for efficient state management and fetching.
    • Internationalization: Multilingual support via React i18next.
    • Theming: Built-in multi-theming configuration without additional dependencies.
    • Secure Storage: High-performance key-value storage using React Native MMKV.
    • Environment Management: Pre-installed tools for handling environment variables.
  3. Identify key configuration files in the React Native Boilerplate

    The project root and src directory contain the following essential configuration and entry files:

    • .env: Environment variables configuration.
    • jest.config.js: Configuration for the Jest testing framework.
    • jest.setup.js: Configuration for Jest mocking.
    • tsconfig.json: TypeScript configuration.
    • src/App.{js,tsx}: The main entry component of the application.
  4. Implement a screen with error handling using SafeScreen

    The SafeScreen component is a helper used to wrap screen content. It automatically provides a safe area view and a status bar. Its primary feature is the ability to display a fallback UI (via the DefaultError component) when an error occurs, either through a passed isError prop or via the application's error boundary system.
    import { useI18n, useUser } from '@/hooks';
    import { SafeScreen } from '@/components/templates';
    
    function Example() {
      const { useFetchOneQuery } = useUser();
      const fetchOneUserQuery = useFetchOneQuery(1);
    
      return (
        <SafeScreen
          isError={fetchOneUserQuery.isError} // If true, the fallback UI is displayed
          onResetError={fetchOneUserQuery.refetch} // Function to reset error state and retry
        >
          {/* Your screen content here */}
        </SafeScreen>
      );
    }
  5. Define component-specific styles using the theme

    To keep the theme configuration as the single source of truth and avoid inline styles in components, use the components file in the theme directory. This allows you to create style objects for specific components that leverage the theme's layout, backgrounds, and fonts.
    export default ({ layout, backgrounds, fonts }: ComponentTheme) => {
      return {
        buttonCircle: {
          ...layout.justifyCenter,
          ...layout.itemsCenter,
          ...backgrounds.purple100,
          ...fonts.gray400,
          height: 70,
          width: 70,
          borderRadius: 35,
        },
      } as const satisfies Record<string, ImageStyle | TextStyle | ViewStyle>;
    };
  6. Prerequisites for Android beta distribution

    Before setting up Fastlane for Android, ensure the following are completed:

    1. Have a Google Play Console admin account (email and password).
    2. Manually create the application in the Google Play Console (Fastlane cannot create the app entry).
    3. Use the correct .gitignore file in the android directory.
    4. Collect Google Credentials: In the Google Play Console, add &hl=en to the URL to ensure English language settings, create a Service Account, download the JSON key file, and place it at my-project/android/key.json.
    5. Install all React Native dependencies for macOS and Android.