首页 > 解决方案 > React Native 安装包

问题描述

React-Native 新手在这里遇到问题,想知道我是否错过了安装软件包的基本步骤。

我一直在尝试使用 react-native-rsa-native 包(https://github.com/amitaymolko/react-native-rsa-native

但是我得到

TypeError: undefined is not an object (evaluating '_reactNativeRsaNative.RSA.generateKeys')

我的测试代码是:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

import { RSA, RSAKeychain } from 'react-native-rsa-native';

export default class App extends React.Component {

  componentWillMount() {
    RSA.generateKeys(4096) // set key size
      .then(keys => {
        console.log('4096 private:', keys.private) // the private key
        console.log('4096 public:', keys.public) // the public key
      });
  }

  render() {
    ...
  }
}

遵循的步骤是:

expo init rsatest --template blank@sdk-31 --yarn
cd rsatest
yarn add react-native-rsa-native
react-native link react-native-rsa-native
yarn start

react-native-rsa-native 包确实出现在 node_modules 目录中,但是我觉得我错过了一些东西。

有任何想法吗?

标签: react-nativepackage

解决方案


可能相关的一件事是 componentWillMount 已被弃用。

https://reactjs.org/docs/react-component.html React 文档是 React Native 文档的基础。

同样使用componentWillMount()如果有 fetch 调用,使用 promise,promise 在组件挂载之前不会返回,组件将至少返回一次空。

我建议尝试componentDidMount()

希望这可以帮助

拼写编辑*


推荐阅读