React Native Installation
Before you start #
Prepare environment #
Install the dependencies and set up your environment. A new app: npx @react-native-community/cli@latest init BarkoderApp, as in the General example.
1. Add the package #
Add the package #
Install the package from npm, or from a folder you downloaded.
Add package from npm #
From npm, with Yarn:
yarn add barkoder-react-native
or with npm:
npm install barkoder-react-native
Add package from a local folder #
For a local package, download it from Downloads in the barKoder Developer Portal, extract it and add it by its path, with Yarn:
yarn add <path to barkoder-react-native package>
or with npm:
npm install <path to barkoder-react-native package>
For iOS, install the native dependencies, in the app's folder:
cd ios
pod install
cd ..
Import package #
Import it in the file that shows the scanner:
import { Barkoder, BarkoderView } from 'barkoder-react-native';
import React from 'react';
2. Allow the camera #
Camera permissions #
The scanner needs the camera.
- Android: the package's manifest asks for it.
-
iOS: add the camera key to your project's
Info.plist:
<key>NSCameraUsageDescription</key>
<string>Camera permission</string>
3. Add the scanner view #
BarkoderView #
Put BarkoderView in your layout and give it a size with style (the camera preview fills it). Set:
- licenseKey: your license key
- onBarkoderViewCreated: called when the SDK is ready (step 5)
<BarkoderView
style={{ width: '100%', height: 400 }}
licenseKey="KEY"
onBarkoderViewCreated={onBarkoderViewCreated}
/>
Step 4 says where to get a license key.
4. Set the license key and barcode types #
Licensing #
(Unlicensed) and part of its data is replaced with dots. Get a free trial key on the trial page: choose Native or WASM and enter your app ID (for the Web SDK, your domain). A trial key lasts 30 days, for up to 25 devices, one per app ID. For a production license, request a quote. By using the SDK you agree to the End User License Agreement.The key goes in the licenseKey of BarkoderView (step 3). The barcode types are turned on in onBarkoderViewCreated, in the code of step 5.
5. Start scanning and read results #
Start scanning #
When onBarkoderViewCreated is called, the SDK is ready: turn on the barcode types there and keep the instance. Add this state and these handlers inside your function component, before its return, in a TSX file, and call scanPressed when the user wants to scan, for example from a button:
const [barkoder, setBarkoder] = React.useState<Barkoder | null>(null);
function onBarkoderViewCreated(_barkoder: Barkoder) {
_barkoder.setBarcodeTypeEnabled(Barkoder.BarcodeType.qr, true);
_barkoder.setRegionOfInterestVisible(true);
setBarkoder(_barkoder);
}
function scanPressed() {
if (!barkoder) return;
barkoder.startScanning((result) => {
// Handle the scan result here.
});
}