You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo应用中GooglePlacesAutocomplete报错:crypto.getRandomValues() not supported

问题:Expo应用使用react-native-google-places-autocomplete时出现crypto.getRandomValues()不支持错误

基本信息

  • 组件版本:"react-native-google-places-autocomplete": "^2.5.7"
  • 运行环境:Android 15模拟器

操作步骤

  1. 执行安装命令:
    npx expo install react-native-google-places-autocomplete
    
  2. 在页面中使用组件,代码如下:
    import React from 'react';
    import { View } from 'react-native';
    import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
    
    const CreateProfile = () => {
        return (
            <View style={{ flex: 1, padding: 10 }}>
                <GooglePlacesAutocomplete
                    placeholder="Enter address"
                    minLength={2} 
                    fetchDetails={true}
                    onPress={(data, details = null) => {
                        console.log(data, details);
                    }}
                    query={{
                        key: "<apiKey>",
                        language: 'en', 
                        components: 'country:us',
                    }}
                    styles={{
                        textInputContainer: {
                            width: '100%',
                        },
                        textInput: {
                            height: 50,
                            color: '#5d5d5d',
                            fontSize: 16,
                        },
                        predefinedPlacesDescription: {
                            color: '#1faadb',
                        },
                    }}
                    nearbyPlacesAPI="GooglePlacesSearch"
                    debounce={200}
                />
            </View>
        );
    };
    
    export default CreateProfile;
    

报错信息

ERROR  Error: crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported

This error is located at:
    in GooglePlacesAutocomplete (created by CreateProfile)
    in RCTView (created by View)
    in View (created by CreateProfile)
    in CreateProfile (created by SceneView)
    ...(省略后续组件栈内容)
    in main(RootComponent), js engine: hermes

已尝试的无效操作

执行命令 npx expo install @types/react-native-get-random-values 并在index.tsx顶部导入,出现“Render Error: Native module not found”错误。


解决方案

  1. 安装正确的核心依赖包:
    npx expo install react-native-get-random-values
    
  2. 在项目入口文件顶部导入:
    在index.tsx或App.tsx的最顶部添加导入语句:
    import 'react-native-get-random-values';
    
  3. 清除缓存并重启开发服务器:
    npx expo start --clear
    

原因说明

react-native-google-places-autocomplete依赖的uuid库需要crypto.getRandomValues()API,而Expo默认环境未提供该API支持。之前错误安装了类型定义包而非核心功能包,导致原生模块找不到,安装正确的react-native-get-random-values即可补全API支持。


内容的提问来源于stack exchange,提问作者Rushwin Jamdas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 20:15:58