Template version: v0.2.1
[!TIP] GitHub address
Find the matching version information in the release address of a third-party library:@react-native-oh-tpl/react-native-get-random-values.For older versions that are not published to npm, please refer to the installation guide to install the tgz package.
Go to the project directory and execute the following instruction:
npm install @react-native-oh-tpl/react-native-get-random-valuesyarn add @react-native-oh-tpl/react-native-get-random-valuesThe following code shows the basic use scenario of the repository:
[!WARNING] The name of the imported repository remains unchanged.
import React, { useState } from "react";
import { View, Text, StyleSheet, Button } from "react-native";
import "react-native-get-random-values";
export const GetRandomValues = () => {
const [randomValue, setRandomValue] = useState < any > [];
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
},
});
const clickBtn = () => {
const getRandomValues = global?.crypto?.getRandomValues(new Uint8Array(4));
console.log(JSON.stringify(getRandomValues), "click");
const array = Object.values(getRandomValues);
console.log(array, "click");
setRandomValue(array);
};
return (
<View style={styles.container}>
<Text style={{ fontSize: 20 }}> click to get random number </Text>
<Button onPress={clickBtn} title="click" />
{randomValue?.map((item: any, index: number) => {
return <Text key={index}>{item}</Text>;
})}
</View>
);
};Currently, HarmonyOS does not support AutoLink. Therefore, you need to manually configure the linking.
Open the harmony directory of the HarmonyOS project in DevEco Studio.
{
...
"overrides": {
"@rnoh/react-native-openharmony" : "./react_native_openharmony"
}
}Currently, two methods are available:
Method 1 (recommended): Use the HAR file.
[!TIP] The HAR file is stored in the
harmonydirectory in the installation path of the third-party library.
Open entry/oh-package.json5 file and add the following dependencies:
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-oh-tpl/react-native-get-random-values": "file:../../node_modules/@react-native-oh-tpl/react-native-get-random-values/harmony/get_random_values.har"
}Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm installMethod 2: Directly link to the source code.
[!TIP] For details, see Directly Linking Source Code.
Open the entry/src/main/ets/RNPackagesFactory.ts file and add the following code:
...
+ import { RNGetRandomValuesPackage } from "@react-native-oh-tpl/react-native-get-random-values/ts";
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new RNGetRandomValuesPackage(ctx)
];
}Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm installThen build and run the code.
This document is verified based on the following versions:
- RNOH: 0.72.20-CAPI; SDK: HarmonyOS NEXT Developer Preview2; IDE: DevEco Studio 4.1.3.700; ROM: 3.0.0.19;
[!TIP] The Platform column indicates the platform where the properties are supported in the original third-party library.
[!TIP] If the value of HarmonyOS Support is yes, it means that the HarmonyOS platform supports this property; no means the opposite; partially means some capabilities of this property are supported. The usage method is the same on different platforms and the effect is the same as that of iOS or Android.
Name Description Type Required Platform HarmonyOS Support global.crypto.getRandomValues lets you get cryptographically strong random values. funtion no IOS/Android yes
This project is licensed under The MIT License (MIT).