计算 RNRandomBytes.seed 时如何解决 'Null is not an object' 错误

How do I resolve 'Null is not an object' error when evaluating RNRandomBytes.seed

我正在尝试使用 expo 和 react-native 构建一个移动应用程序,我有一个屏幕,我需要为用户生成一个密码短语,我正在使用 react-native-bip39 来做到这一点.

安装以下软件包后:react-native-bip39、react-native-crypto 和 react-native-randombytes,出现错误:

Unable to resolve module stream

经过一些挖掘,我了解到我可以使用 rn-nodeify 来安装丢失的包,但是当我安装它时,出现了以下错误,我似乎找不到解决方案:

TypeError: null is not an object (evaluating 'RNRandomBytes.seed') at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule at http://192.168.18.160:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false:222696:3 in global code

终端上还有一个:

Invariant Violation: "main" has not been registered. This can happen if: Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project. A module failed to load due to an error and AppRegistry.registerComponent wasn't called. at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError

在根文件夹中有一个shim.js由rn-nodeify生成的文件,并且package.json有更多由同一工具安装的依赖项。

我该如何解决这个问题?

我遇到了同样的问题,经过一番研究,这对我有用:

1- 添加缓冲区和事件

2- 种子问题:从 react-native-randombytes 切换到 react-native-get-random-values TypeError: null is not an object (evaluating 'RNRandomBytes.seed') React Native

3- 通过添加 'patch-package' 修复 cipher-base 和 readable-stream 中的流问题 https://github.com/crypto-browserify/cipher-base/issues/10

最后,这就是我在自定义挂钩中实现解决方案以获取随机单词的方式:

import { useState, useEffect } from "react";
import "react-native-get-random-values";
import { entropyToMnemonic } from "bip39";

export default function UseBip39() {
  const [state, setState] = useState([]);

  useEffect(() => {
    async function generateWords() {
      const entropy = await crypto.getRandomValues(new Uint8Array(16));
      setState(entropyToMnemonic(entropy).split(" "));
    }
    generateWords();
  }, []);

  return state;
}