TheCodingMachine React Native Boilerplate

repository·main·Indexed 26 days ago

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

A lightweight, scalable React Native boilerplate designed to separate UI from business logic. It features a pre-configured stack including TanStack Query for data fetching, React Native MMKV for high-performance storage, React Navigation for routing, and react-i18next for internationalization. The project supports both JavaScript and TypeScript, utilizes Atomic Design for component organization, and includes a built-in multi-theming system managed via a central configuration file.

Tokens
17.4K
Snippets
46
Records
123
Agent score
90%

What's inside @thecodingmachine/react-native-boilerplate

  1. Overview of React Native Boilerplate features

    main

    The React Native Boilerplate is a lightweight, scalable template designed for mobile app development with a focus on simplicity and separation of concerns. Key features include:

    • Language Choice: Support for both JavaScript and TypeScript.
    • Navigation: Pre-configured with React Navigation.
    • Data Fetching: Powered by TanStackQuery and Ky for HTTP requests.
    • Internationalization: Multilingual support via React i18next.
    • Theming: Built-in multi-theming system without extra dependencies.
    • Storage: Secure and efficient key-value storage using React Native MMKV.
    • Environment Management: Tools for handling environment variables are pre-installed.
  2. Data fetching and storage in version 4.0.0+

    main

    The boilerplate has moved away from Redux to provide a more lightweight and less opinionated stack. For version 4.0.0 and later, the following libraries are used:

    • Data Fetching: React Query is used for managing asynchronous data and server state.
    • Data Storage: React Native MMKV is used for high-performance, secure, and synchronous local data storage.
  3. Understand the boilerplate folder structure

    main

    The project is organized into several top-level directories within src/ to ensure separation of concerns:

    • src/components: Presentational components organized using Atomic Design.
    • src/hooks: Custom React hooks.
    • src/navigation: Navigation components and navigator definitions.
    • src/screens: Components representing full app screens.
    • src/services: Data fetching logic and related services.
    • src/theme: Application theme configuration.
    • src/translations: Language support and localization configuration.
  4. Manage environment variables using .env files

    main

    The boilerplate comes pre-configured with dotenv and babel-plugin-inline-dotenv to allow easy management of environment variables.

    1. Define your variables in the .env file at the root of your project using the KEY=VALUE format.
    2. Access these variables directly in your JavaScript or TypeScript code via process.env.KEY.
    API_URL=https://myapi.com
    process.env.API_URL
  5. Use the SafeScreen component

    main

    The SafeScreen component is a template helper that provides a SafeAreaView, manages the StatusBar, and implements a fallback UI (using the DefaultError component) when an error occurs. It is designed to wrap screen content and handle error states gracefully using an error boundary system.

    To use it, pass an isError boolean to trigger the fallback UI and an onResetError function to allow the user to attempt to recover from the error (e.g., by refetching data).

    import { useI18n, useUser } from '@/hooks';
    import { SafeScreen } from '@/components/templates';
    
    function Example() {
      const { useFetchOneQuery } = useUser();
      const fetchOneUserQuery = useFetchOneQuery(1);
    
      return (
        <SafeScreen
          isError={fetchOneUserQuery.isError} // boolean value to determine whether an error occurred or not
          onResetError={fetchOneUserQuery.refetch} // function to reset the error state and re-execute the query
        >
          // your content here
        </SafeScreen>
      );
    }
  6. Use the ErrorBoundary component

    main

    The ErrorBoundary component is a helper organism used to catch JavaScript errors within a component tree and display a fallback UI. This prevents the entire application from crashing and allows you to show a user-friendly error state.

    Wrap your component tree with <ErrorBoundary> and provide a fallback prop containing the UI to display when an error is caught.

    import { ErrorBoundary } from "@/components/atoms";
    
    <ErrorBoundary fallback={<div>Something went wrong</div>}>
      <ExampleApplication />
    </ErrorBoundary>