undefined不是对象(评估'_expo.default.Constants')

cha*_*dra 2 react-native manifest.json expo

我想在移动设备/模拟器上进行调试并将其动态指向API。

const {manifest} = Expo.Constants; 
const api = manifest.packagerOpts.dev
? manifest.debuggerHost.split(':').shift().concat(':3000')
: 'productionurl.com'


export function getEvents(){
    return fetch('http://${api}/events')
    .then(response => response.json())
    .then(events => events.map(e =>({ ...e, date: new Date(e.date)})));
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

未定义不是对象(正在评估'_expo.default.Constants')

hon*_*lop 6

带来展会的完整模块:

import * as Expo from 'expo'
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 6

我一起工作"sdkVersion": "35.0.0"。看来Expo改变了它的API。他们提取了一些零件并将其移至单独的包装中。现在,对于这种情况,您需要安装一个单独的依赖项:

npm i --save expo-constants
Run Code Online (Sandbox Code Playgroud)

然后FileSystem为您的组件独立导入对象:

import Constants from 'expo-constants';
Run Code Online (Sandbox Code Playgroud)