【问题标题】:React 16.13.1 Hooks error `React.useState is not a function`React 16.13.1 Hooks 错误`React.useState 不是函数`
【发布时间】: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


【解决方案1】:

看看有没有帮助?

import React, { useState } from 'react';

const [isLoadingComplete, setLoadingComplete] = useState(false);

【讨论】:

猜你喜欢
  • 2019-04-16
  • 2021-04-07
  • 2020-01-27
  • 2022-08-07
  • 1970-01-01
  • 2019-11-29
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多