Skip to content

React Native Boilerplate with modern design from kholiq

License

Notifications You must be signed in to change notification settings

venxik/react-native-boilerplate

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Œ React Native Boilerplate

Quality Gate Status Coverage CD-Distribute Android to Firebase CI-Check Commit Message & SonarCloud CI-Android CI-iOS

React Native boilerplate repository version 0.0.1

⚑️Getting Started

Make sure you already React Native environment running in you machine. Please refer to the official docs

βš’οΈ Configurations

  1. Change package name, bundle id (iOS) or application id (android). Reference:
  2. Create your own keystore and key.properties. Reference:
  3. Setup your firebase project for firebase analytics, crashlytics, and messaging. Reference:
  4. Setup your android and ios project to integrate with Fastlane and Firebase Reference:
  5. Configure your env with react-native-dotenv
  6. Using Atomic Design Pattern
  7. Using Redux, Redux Toolkit and RTK Query for services
  8. Using Native Base UI material to design this boilerplate

🚚 How to run, and build Apk

NOTES: This boilerplate uses yarn as main command, not npm.

To make sure android emulator have the same port with metro bundler, run this command first

yarn android:local

Example how to run android with development env

yarn android:local
yarn run:android-debug

Example how to run android with staging env

yarn android:local
yarn run:android-staging

Example how to run android with production env

yarn android:local
yarn run:android-prod

If there is any error when running these commands, try to run with Android Studio

Example how to build android with prod env release variant

yarn release:android

If there is any error when running this command, try to build with Android Studio

Example how to run ios with development env

yarn run:ios-debug

Example how to run ios with staging env

yarn run:ios-staging

Example how to run ios with production env

yarn run:ios-prod

If there is any error when running these commands, try to run with Xcode

βš™οΈ Supported Flavor

  1. development
  2. production
  3. staging

In android there are 3 product flavor: dev, prod, staging. In iOS there are 3 scheme: Boilerplate, Boilerplate Prod, Boilerplate Staging. All of them are already correspond with the env.

πŸ†• Versioning

Using Semantic Versioning 2.0.0

Major.Minor.Patch

Given a version number MAJOR.MINOR.PATCH, increment the:

  1. MAJOR version when you make incompatible API changes,
  2. MINOR version when you add functionality in a backwards compatible manner, and
  3. PATCH version when you make backwards compatible bug fixes. Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

πŸ”  Naming Convention

camelCase for file and folder.

πŸ–οΈ Theme

Using Native-Base Theme, we can create and/or modify existing variant of the component. It also supports dark mode. Here's example of adding dark mode style and add new variant to Button component.

const Button = {
  // You can insert dark theme mode here
  // mode(first, second) -> first is for light theme, second is for dark theme
  baseStyle: (props: any) => {
    return {
      color: themeTools.mode('red.300', 'blue.300')(props),
    };
  },
  // You can also define or modify existing variant of component here
  variants: {
    solid: (props: any) => {
      return {
        _text: {
          fontFamily: props.fontFamily,
          fontWeight: props.fontWeight,
        },
        ...props,
      };
    },
    outlineLime: (props: any) => {
      return {
        borderWidth: '1',
        borderColor: 'current.100',
        _text: {
          color: 'current.100',
          fontWeight: props.fontWeight,
          fontFamily: props.fontFamily,
        },
        ...props,
      };
    },
  },
};
// You need to insert this object to the native-base's extendTheme function

🌲 At this point, your project layout should look like this:

react-native-boilerplate
β”œβ”€ .buckconfig
β”œβ”€ .bundle
β”‚  └─ config
β”œβ”€ .commitlintrc.json
β”œβ”€ .eslintignore
β”œβ”€ .eslintrc.js
β”œβ”€ .github
β”‚  └─ workflows
β”‚     └─ build.yml
β”œβ”€ .gitignore
β”œβ”€ .husky
β”‚  β”œβ”€ _
β”‚  β”‚  β”œβ”€ .gitignore
β”‚  β”‚  └─ husky.sh
β”‚  β”œβ”€ commit-msg
β”‚  └─ pre-commit
β”œβ”€ .prettierignore
β”œβ”€ .prettierrc.js
β”œβ”€ .ruby-version
β”œβ”€ .vscode
β”‚  β”œβ”€ .react
β”‚  └─ settings.json
β”œβ”€ .watchmanconfig
β”œβ”€ CHANGELOG.md
β”œβ”€ Gemfile
β”œβ”€ LICENSE
β”œβ”€ __mocks__
β”‚  β”œβ”€ @react-native-async-storage
β”‚  β”‚  └─ async-storage.ts
β”‚  β”œβ”€ helpers.tsx
β”‚  β”œβ”€ react-i18next
β”‚  β”‚  └─ index.js
β”‚  β”œβ”€ timeTravel.ts
β”‚  └─ wrapper.tsx
β”œβ”€ android
β”‚  β”œβ”€ Gemfile
β”‚  β”œβ”€ Gemfile.lock
β”‚  β”œβ”€ fastlane
β”‚  β”‚  β”œβ”€ .env
β”‚  β”‚  β”œβ”€ Appfile
β”‚  β”‚  β”œβ”€ Fastfile
β”‚  β”‚  β”œβ”€ Pluginfile
β”‚  β”‚  β”œβ”€ README.md
β”‚  β”‚  └─ report.xml
β”‚  β”œβ”€ gradle
β”‚  β”‚  └─ wrapper
β”‚  β”‚     β”œβ”€ gradle-wrapper.jar
β”‚  β”‚     └─ gradle-wrapper.properties
β”‚  β”œβ”€ gradle.properties
β”‚  β”œβ”€ gradlew
β”‚  └─ gradlew.bat
β”œβ”€ app.json
β”œβ”€ babel.config.json
β”œβ”€ index.js
β”œβ”€ ios
β”‚  β”œβ”€ Config.xcconfig
β”‚  β”œβ”€ Gemfile
β”‚  β”œβ”€ Gemfile.lock
β”‚  β”œβ”€ Podfile
β”‚  β”œβ”€ Podfile.lock
β”‚  └─ fastlane
β”‚     β”œβ”€ Appfile
β”‚     β”œβ”€ Fastfile
β”‚     └─ Pluginfile
β”œβ”€ jest.config.js
β”œβ”€ jest.setup.js
β”œβ”€ metro.config.js
β”œβ”€ package.json
β”œβ”€ react-native.config.js
β”œβ”€ sonar-project.properties
β”œβ”€ src
β”‚  β”œβ”€ __tests__
β”‚  β”‚  └─ App.test.tsx
β”‚  β”œβ”€ assets
β”‚  β”‚  β”œβ”€ fonts
β”‚  β”‚  β”‚  β”œβ”€ OpenSans
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-Bold.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-BoldItalic.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-ExtraBold.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-ExtraBoldItalic.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-Italic.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-Light.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-LightItalic.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-Medium.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-MediumItalic.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-Regular.ttf
β”‚  β”‚  β”‚  β”‚  β”œβ”€ OpenSans-SemiBold.ttf
β”‚  β”‚  β”‚  β”‚  └─ OpenSans-SemiBoldItalic.ttf
β”‚  β”‚  β”‚  └─ Poppins
β”‚  β”‚  β”‚     β”œβ”€ OFL.txt
β”‚  β”‚  β”‚     β”œβ”€ Poppins-Black.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-BlackItalic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-Bold.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-BoldItalic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-ExtraBold.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-ExtraBoldItalic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-ExtraLight.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-ExtraLightItalic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-Italic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-Light.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-LightItalic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-Medium.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-MediumItalic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-Regular.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-SemiBold.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-SemiBoldItalic.ttf
β”‚  β”‚  β”‚     β”œβ”€ Poppins-Thin.ttf
β”‚  β”‚  β”‚     └─ Poppins-ThinItalic.ttf
β”‚  β”‚  β”œβ”€ images
β”‚  β”‚  β”‚  β”œβ”€ common
β”‚  β”‚  β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”‚  β”‚  β”‚  β”œβ”€ logo.png
β”‚  β”‚  β”‚  β”‚  └─ logo.svg
β”‚  β”‚  β”‚  └─ index.ts
β”‚  β”‚  └─ index.ts
β”‚  β”œβ”€ components
β”‚  β”‚  β”œβ”€ atoms
β”‚  β”‚  β”‚  └─ index.ts
β”‚  β”‚  β”œβ”€ containers
β”‚  β”‚  β”‚  └─ index.ts
β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”‚  β”œβ”€ molecules
β”‚  β”‚  β”‚  β”œβ”€ ProductsCard
β”‚  β”‚  β”‚  β”‚  └─ index.tsx
β”‚  β”‚  β”‚  β”œβ”€ UsersCard
β”‚  β”‚  β”‚  β”‚  └─ index.tsx
β”‚  β”‚  β”‚  └─ index.ts
β”‚  β”‚  └─ organisms
β”‚  β”‚     β”œβ”€ FOProductsSection
β”‚  β”‚     β”‚  └─ index.tsx
β”‚  β”‚     β”œβ”€ FOUserSection
β”‚  β”‚     β”‚  └─ index.tsx
β”‚  β”‚     └─ index.ts
β”‚  β”œβ”€ config
β”‚  β”‚  β”œβ”€ config.d.ts
β”‚  β”‚  β”œβ”€ constant.ts
β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”‚  β”œβ”€ setting.ts
β”‚  β”‚  └─ url.ts
β”‚  β”œβ”€ global
β”‚  β”‚  β”œβ”€ env.d.ts
β”‚  β”‚  β”œβ”€ index.d.ts
β”‚  β”‚  └─ navigation.d.ts
β”‚  β”œβ”€ hooks
β”‚  β”‚  β”œβ”€ __tests__
β”‚  β”‚  β”‚  └─ useLayout.test.ts
β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”‚  β”œβ”€ useDebounce.ts
β”‚  β”‚  └─ useLayout.ts
β”‚  β”œβ”€ index.tsx
β”‚  β”œβ”€ lang
β”‚  β”‚  β”œβ”€ en.json
β”‚  β”‚  └─ id.json
β”‚  β”œβ”€ navigation
β”‚  β”‚  β”œβ”€ __tests__
β”‚  β”‚  β”‚  └─ navigation.test.tsx
β”‚  β”‚  β”œβ”€ index.tsx
β”‚  β”‚  └─ navigationService.ts
β”‚  β”œβ”€ redux
β”‚  β”‚  β”œβ”€ __tests__
β”‚  β”‚  β”‚  └─ application.test.ts
β”‚  β”‚  β”œβ”€ application
β”‚  β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”‚  β”‚  └─ types.d.ts
β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”‚  └─ rootReducer.ts
β”‚  β”œβ”€ screens
β”‚  β”‚  β”œβ”€ Home
β”‚  β”‚  β”‚  β”œβ”€ hook.ts
β”‚  β”‚  β”‚  β”œβ”€ index.tsx
β”‚  β”‚  β”‚  β”œβ”€ styles.ts
β”‚  β”‚  β”‚  └─ types.d.ts
β”‚  β”‚  β”œβ”€ Setting
β”‚  β”‚  β”‚  β”œβ”€ hook.ts
β”‚  β”‚  β”‚  β”œβ”€ index.tsx
β”‚  β”‚  β”‚  β”œβ”€ styles.ts
β”‚  β”‚  β”‚  └─ types.d.ts
β”‚  β”‚  β”œβ”€ SignIn
β”‚  β”‚  β”‚  β”œβ”€ hook.ts
β”‚  β”‚  β”‚  β”œβ”€ index.tsx
β”‚  β”‚  β”‚  β”œβ”€ styles.ts
β”‚  β”‚  β”‚  └─ types.d.ts
β”‚  β”‚  β”œβ”€ Splash
β”‚  β”‚  β”‚  β”œβ”€ hook.ts
β”‚  β”‚  β”‚  β”œβ”€ index.tsx
β”‚  β”‚  β”‚  β”œβ”€ styles.ts
β”‚  β”‚  β”‚  └─ types.d.ts
β”‚  β”‚  β”œβ”€ __tests__
β”‚  β”‚  β”‚  β”œβ”€ Setting.test.tsx
β”‚  β”‚  β”‚  β”œβ”€ SignIn.test.tsx
β”‚  β”‚  β”‚  └─ Splash.test.tsx
β”‚  β”‚  └─ index.tsx
β”‚  β”œβ”€ services
β”‚  β”‚  β”œβ”€ baseQuery.ts
β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”‚  β”œβ”€ products
β”‚  β”‚  β”‚  └─ index.ts
β”‚  β”‚  └─ user
β”‚  β”‚     └─ index.ts
β”‚  β”œβ”€ theme
β”‚  β”‚  β”œβ”€ __tests__
β”‚  β”‚  β”‚  β”œβ”€ components.test.ts
β”‚  β”‚  β”‚  └─ fonts.test.ts
β”‚  β”‚  β”œβ”€ baseStyle.ts
β”‚  β”‚  β”œβ”€ colors.ts
β”‚  β”‚  β”œβ”€ components.ts
β”‚  β”‚  β”œβ”€ fonts.ts
β”‚  β”‚  └─ index.ts
β”‚  └─ utils
β”‚     β”œβ”€ __tests__
β”‚     β”‚  β”œβ”€ generic.test.ts
β”‚     β”‚  └─ normalize.test.ts
β”‚     β”œβ”€ generic.ts
β”‚     └─ normalize.ts
β”œβ”€ tsconfig.json
└─ yarn.lock

A brief description of the layout:

  • .github has one github workflows directory.
  • android is android configuration directory.
  • ios is ios configuration directory.
  • .gitignore varies per project, but most of it uses create react-native app base .gitignore file.

RTK Query example fetch data

You can see the example in src/services/products. This is the only basic api to get data from API, you can learn more from the official docs.

import { createApi } from '@reduxjs/toolkit/dist/query/react';
import { functionsBaseQuery } from '../baseQuery';

export interface IProductsDetail {
  id: number;
  title: string;
  description: string;
  price: number;
  discountPercentage: number;
  rating: number;
  stock: number;
  brand: string;
  category: string;
  thumbnail: string;
  images: string[];
}

export interface IProducts {
  products: IProductsDetail[];
  total: number;
  skip: number;
  limit: number;
}

const reducerPath = 'productsAPI';

export const productsAPI = createApi({
  reducerPath: reducerPath,
  baseQuery: functionsBaseQuery(),
  tagTypes: ['Products'], //Provide tags that are available for this api
  keepUnusedDataFor: process.env.NODE_ENV !== 'test' ? 60 : 0,
  endpoints: (builder) => ({
    getProduct: builder.query<IProducts, string>({ //Rule of thumb, query is used for GET method
      query: (query) => `/products/search?q=${query}`,
      providesTags: ['Products'], //Provide the corresponding tags from tagTypes
    }),
    refetchProducts: builder.mutation<null, void>({ //Rule of thumb, mutation is used for POST, PATCH, DELETE method
      // The query is not relevant here, so a `null` returning `queryFn` is used
      queryFn: () => ({ data: null }),
      // This mutation takes advantage of tag invalidation behaviour to trigger
      // any queries that provide the 'Post' or 'User' tags to re-fetch if the queries
      // are currently subscribed to the cached data.
      // Meaning if we call this, it will call getProduct to update the cached data
      invalidatesTags: ['Products'],
    }),
  }),
});

export const { useGetProductQuery } = productsAPI;
export const productsQueryReducer = { [reducerPath]: productsAPI.reducer };

πŸ’« Upload "beta" version to Firebase with Fastlane

This is only for android. To upload a "beta" version to firebase, you can run command

  android:beta

😈 Unit Testing

Unit testing uses jest. You can navigate to here to see some unit test examples

πŸ§ͺ How to Test Coverage

Run yarn test:cov and it will generate coverage report on .coverage folder

😈 Detox end-to-end testing

This boilerplate is already supported with Detox. You can navigate here to see some examples. All available configs are on .detoxrc.json file. You need to change your simulator and emulator name on the .detoxrc.json configuration first to be same with the one on your machine so it can work on your machine.

To build and test application run on ios

build:e2e-ios-dev-debug
test:e2e-ios-dev-debug

To build and test application run on android

build:e2e-android-dev-debug
test:e2e-android-dev-debug

😈 Jest example to mock service response using jest-fetch-mock

You can see the example in src/services/__tests__/products.test.ts based on this article.

import { AllTheProviders } from '../../__mocks__/utils/wrapper';
import { renderHook } from '@testing-library/react-hooks/native';
import { useGetProductQuery } from '../../services';
import { products } from '../../__mocks__/testData';

const updateTimeout = 5000;

describe('FOProductsSection screen', () => {
  it('handles good response', async () => {
    fetchMock.mockResponse(JSON.stringify({ data: products }));
    const { result, waitForNextUpdate } = renderHook(() => useGetProductQuery(undefined), {
      wrapper: AllTheProviders,
    });
    const initialResponse = result.current;
    expect(initialResponse.data).toBeUndefined();
    expect(initialResponse.isLoading).toBe(true);
    await waitForNextUpdate({ timeout: updateTimeout });

    const nextResponse = result.current;
    expect(nextResponse.data).toBeDefined();
    expect(nextResponse.isLoading).toBe(false);
    expect(nextResponse.isSuccess).toBe(true);
  });

  it('handles error response', async () => {
    fetchMock.mockReject(new Error('Internal Server Error'));
    const { result, waitForNextUpdate } = renderHook(() => useGetProductQuery(undefined), {
      wrapper: AllTheProviders,
    });
    const initialResponse = result.current;
    expect(initialResponse.data).toBeUndefined();
    expect(initialResponse.isLoading).toBe(true);

    await waitForNextUpdate({ timeout: updateTimeout });

    const nextResponse = result.current;
    expect(nextResponse.data).toBeUndefined();
    expect(nextResponse.isLoading).toBe(false);
    expect(nextResponse.isError).toBe(true);
  });
});

😈 Jest example to mock service response for component

You can see the example in src/components/organisms/__tests__/FOProductsSection.test.tsx based on this article.

import React from 'react';
import * as hooks from '../../../services/products';
import FOProductsSection from '../FOProductsSection';
import { render } from '../../../__mocks__/utils/wrapper';
import { products } from '../../../__mocks__/testData';

describe('FOProductsSection screen', () => {
  it('can shows 4 data correctly', async () => {
    jest.spyOn(hooks, 'useGetProductQuery').mockReturnValue({
      data: products,
      isError: false,
      isLoading: false,
      refetch: function (): void {
        throw new Error('Function not implemented.');
      },
    });
    const { findAllByTestId } = render(<FOProductsSection query={'Apple'} />);
    expect((await findAllByTestId('FMProductsCard')).length).toBe(4);
  });
});

About

React Native Boilerplate with modern design from kholiq

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 65.1%
  • Java 11.6%
  • JavaScript 8.3%
  • C++ 4.8%
  • Ruby 4.1%
  • Objective-C++ 2.5%
  • Other 3.6%