Comprehensive Guide to Patching React Native Packages

Yusuf Sancak
2 min readDec 6, 2023

--

In the realm of React Native development, encountering scenarios where existing packages do not fully meet the project’s requirements is common. Patching is a practical approach to modify such packages. This guide provides a detailed walkthrough on how to effectively patch the react-native-camera package in a React Native project.

Step 1: Environment Setup

Begin by installing the necessary tools for patching. Run the following commands in your terminal to install patch-package and postinstall-postinstall:

npm install --save-dev patch-package postinstall-postinstall

patch-package allows for the modifications of node modules, and postinstall-postinstall automates the application of these patches after installation.

Step 2: Modifying the Package

For example, if the objective is to change the default video quality setting, locate and update the relevant piece of code in the package. Navigate to the node_modules/react-native-camera directory. Here, you will make the necessary modifications.

Consider a scenario where the default video quality in react-native-camera is modified:

Before the patch:

- (instancetype)init {
self.defaultVideoQuality = AVCaptureSessionPresetHigh;
}

After the patch:

- (instancetype)init {
self.defaultVideoQuality = AVCaptureSessionPresetMedium;
}

Step 3: Creating a Patch

After completing the modifications, create a patch file to record these changes. Execute the following command:

npx patch-package react-native-camera

This command generates a patch file and stores it in a patches directory, encapsulating your modifications.

Step 4: Automating Patch Application

To ensure the patch is applied automatically during installation, update your package.json with a postinstallscript:

"scripts": {
"postinstall": "patch-package"
}

This step guarantees that the patch is applied every time npm install is executed, maintaining consistency across installations.

Patching is a powerful tool in customizing React Native packages to fit specific project needs. This guide outlines a systematic approach to applying patches, exemplified through the react-native-camera package. It is important to use this method responsibly, ensuring that patches are maintained and documented for future reference and updates.

--

--

Yusuf Sancak

Hi there! My name is Yusuf, and I'm a software developer based in Izmir, Turkey. With over 5 years of experience in React Native and React.js