开发者问题收集

React Native Expo 与 Moti 出现 TypeError: null 不是对象(评估“dispatcher.useContext”)错误

2022-09-21
1405

我创建了一个新的 Expo 项目来使用不同的动画( npx create-expo-app my-app ),并且安装了 react-native-reanimated 和 moti( npx expo install react-native-reanimated moti )。 导入时没有出现任何问题,但是当我想做一个简单的动画时,出现以下错误:

Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.


TypeError: null is not an object (evaluating 'dispatcher.useContext')

这是我的代码,是一个简单的加载循环:

import * as React from 'react';
import { View } from 'react-native';

//Tried both imports
import { MotiView } from '@motify/components'
import { MotiView } from 'moti'

const LoadingIndicator = ({size}) =>{
    return <MotiView
    from={{
        width:size,
        height:size,
        borderRadius: size /2,
        borderWidth:0,
        shadowOpacity:0.5
    }}
    animate={{
        width:size +20,
        height:size + 20,
        borderRadius: (size +20) /2,
        borderWidth:size / 10,
        shadowOpacity:1
    }}
    transition={{
        type:'timing',
        duration:1000,
        // repeat: Infinity,
        loop:true
    }}
    style={{
        width:size,
        height:size,
        borderRadius: size / 2,
        borderWidth: size / 10,
        borderColor: '#fff',
        shadowColor:'#fff',
        shadowOffset:{width:0, height:0},
        shadowOpacity:1,
        shadowRadius:10
    }}
     />
}

const LoadingScreen= () => {
    return(
        <View style={{
            flex:1,
            alignItems:'center',
            // paddingTop:300,
            justifyContent:'center',
            backgroundColor:'#010100'
        }}
        >
            <LoadingIndicator size={100}/>
        </View>
    )
}

export default LoadingScreen;

我在 App.js 中导入了它:

return(
 {loading ? <LoadingScreen/> : <MainView/>}
)

这是我的 package.json

{
  "name": "simple-qrcode",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eas-build-pre-install": "npm install --save --legacy-peer-deps"
  },
  "dependencies": {
    "expo": "~46.0.9",
    "expo-dev-client": "~1.2.1",
    "expo-status-bar": "~1.4.0",
    "moti": "^0.18.0",
    "react": "18.0.0",
    "react-native": "0.69.5",
    "react-native-reanimated": "~2.9.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9"
  },
  "private": true
}
2个回答

您收到此错误是因为您在功能组件之外使用了钩子,只需在内部进行更改即可修复错误

Ons Jannet
2022-11-21

导致此问题的原因之一是安装了不同版本的 React。我遇到了同样的问题,并按此方法解决了。

如果您使用的是 npm,请运行此命令。

npm install --legacy-peer-deps

然后使用 expo start -c

启动项目

如果您使用的是 yarn,请将其添加到您的 package.json 并再次启动项目:

  "resolutions": {
    "react": "your-version-here"
  }
}
Asmar Ali
2022-12-20