Package Exports
- expo-random
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (expo-random) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
expo-random
expo-random provides a native interface for creating strong random bytes. With Random you can create values equivalent to Node.js core crypto.randomBytes API.
- TypeScript
- Unit-Tests
- Universal Module
- Physical Tests Suite Tests
| 🍎 iOS | 💚 Android | 💻 Web |
|---|---|---|
| ✅ | ✅ | ✅ |
Installation
📚 Expand Installation
First, you need to install the package from npm registry.
npm install expo-random
or
yarn add expo-randomiOS
Podfile: Include the local CocoaPod
👉 Expand Code
pod 'EXRandom', path: '../node_modules/expo-random/ios'Run: $ pod install to sync the pods with XCode.
Android
android/settings.gradle: Make the library accessible to Android
👉 Expand Code
include ':expo-random'
project(':expo-random').projectDir = new File(rootProject.projectDir, '../node_modules/expo-random/android')and if not already included
include ':expo-core'
project(':expo-core').projectDir = new File(rootProject.projectDir, '../node_modules/expo-core/android')android/app/build.gradle: Insert the following lines inside the dependencies block.
👉 Expand Code
api project(':expo-random')and if not already included
api project(':expo-core')./android/app/src/main/java/host/exp/exponent/MainActivity.java: Import, then export the module from your expoPackages:
👉 Expand Code
/**
* At the top of the file.
* This is automatically imported with Android Studio, but if you are in any other editor you will need to manually import the module.
*/
import expo.modules.random.RandomPackage;
// Later in the file...
@Override
public List<Package> expoPackages() {
/* Here you can add your own packages. */
return Arrays.<Package>asList(
/* Include this. */
new RandomPackage()
);
}Notice 🛠 The native installation flow is under maintenance.
Docs
Once installed natively, the module can be accessed from the expo-random package.
Bare React Native
import * as Random from 'expo-random';Expo
import { Random } from 'expo';Methods
getRandomBytesAsync
getRandomBytesAsync(byteCount: number): Promise<Uint8Array>Generates completely random bytes using native implementations. The byteCount property is a number indicating the number of bytes to generate in the form of a Uint8Array.
Parameters
| Name | Type | Description |
|---|---|---|
| byteCount | number |
A number within the range: 0...1024. Anything else will throw a TypeError |
Returns
| Name | Type | Description |
|---|---|---|
| randomBytes | Promise<Uint8Array> |
An array of random bytes with the same length as the byteCount |
Example
const randomBytes = await Random.getRandomBytesAsync(3);Usage
import React from 'react';
import { View } from 'react-native';
import * as Random from 'expo-random';
export default class DemoView extends React.Component {
async componentDidMount() {
const randomBytes = await Random.getRandomBytesAsync(16);
/* Some crypto operation... */
}
render() {
return <View />;
}
}