【发布时间】:2020-11-12 21:43:46
【问题描述】:
我遇到了这个错误
ERROR TypeError: React.useState is not a function. (In 'React.useState(false)', 'React.useState' is undefined)
我正在使用这些依赖项
"dependencies": {
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "~0.63.3",
"expo": "~39.0.2",
"expo-splash-screen": "~0.6.2",
...
}
我认为 Hooks are available 来自 16.8.0 及更高版本。不是吗?有什么问题?
编辑
这里是导致崩溃的代码片段
import { Ionicons } from '@expo/vector-icons';
import * as Font from 'expo-font';
import * as SplashScreen from 'expo-splash-screen';
import * as React from 'react';
export default function useCachedResources() {
const [isLoadingComplete, setLoadingComplete] = React.useState(false);
// Load any resources or data that we need prior to rendering the app
React.useEffect(() => {
async function loadResourcesAndDataAsync() {
try {
SplashScreen.preventAutoHideAsync();
// Load fonts
await Font.loadAsync({
...Ionicons.font,
'space-mono': require('../assets/fonts/SpaceMono-Regular.ttf'),
});
} catch (e) {
// We might want to provide this error information to an error reporting service
console.warn(e);
} finally {
setLoadingComplete(true);
SplashScreen.hideAsync();
}
}
loadResourcesAndDataAsync();
}, []);
return isLoadingComplete;
}
【问题讨论】:
-
您能否发布导致问题的代码行以及您的导入语句?
-
您是否通过
import React from 'react'或import * as React from 'react'包含React? -
你的
react-test-renderer是什么版本? -
@Z.Fralish 编辑并添加了代码的sn-p
-
@Ryan 和
import * as React from 'react';
标签: reactjs react-native react-hooks react-dom