Setup for React Native app on Android and iOS #1096
Unanswered
Ricardo-MT
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When using this library to implement a XMPP client for a React Native app, I can just make it work by overriding the crypto namespace and making sure randomUUID exists with a custom implementation, all this in index.js before booting up the app like this:
// root/index.js
import 'react-native-get-random-values';
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
// Ensure global.crypto exists
if (typeof global.crypto !== 'object') {
global.crypto = {};
}
// Ensure getRandomValues exists (react-native-get-random-values provides it)
if (typeof global.crypto.getRandomValues !== 'function') {
// safety: import again if needed
require('react-native-get-random-values');
}
// Polyfill randomUUID for RN
if (typeof global.crypto.randomUUID !== 'function') {
global.crypto.randomUUID = () => {
const buf = new Uint8Array(16);
global.crypto.getRandomValues(buf);
// Per RFC 4122 §4.4 (version 4)
buf[6] = (buf[6] & 0x0f) | 0x40;
buf[8] = (buf[8] & 0x3f) | 0x80;
const hex = [...buf].map(b => b.toString(16).padStart(2, '0')).join('');
return
${hex.slice(0,8)}-${hex.slice(8,12)}-${hex.slice(12,16)}-${hex.slice(16,20)}-${hex.slice(20)};};
}
AppRegistry.registerComponent(appName, () => App);
Is there a better way of using this package for React Native mobile apps?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions