首页 > 解决方案 > 使用 sha256 反应原生

问题描述

我正在尝试使用这个react-native 库来进行 sha256 散列。这是我的代码:

import React, {Component} from 'react'
import { AppRegistry, Text, View} from 'react-native'
import {sha256} from 'react-native-sha256'

export default class CyrptoTest extends Component {
    constructor(props){
        super(props)
    }

    componentDidMount(){
        sha256("test").then(hash => {
            console.log(hash)
        })
    }

    render(){
            return(
                <View style={{padding:30}}>
                    <Text>this is CryptoTest 1</Text>
                </View>
            )
    }
}

我收到此错误:undefined is not an object (evaluating 'sha256lib.sha256')
我不知道这是为什么或如何解决此问题,感谢任何帮助,谢谢

PS:在我做任何这些之前,我首先在命令行中运行它:

yarn add react-native-sha256
react-native link

为了在我的 Mac 上的模拟器中运行该应用程序,我这样做:

react-native start
react-native run-ios

标签: react-nativesha256

解决方案


我遇到了同样的问题,在我的情况下,我使用的是 react native cli debug server host & port for device,它通过 ./gradlew clean 和重新编译手机上的应用程序来解决。


推荐阅读