skip to Main Content

I created a new Expo project to play with different animations (npx create-expo-app my-app), and I installed react-native-reanimated and moti (npx expo install react-native-reanimated moti).
When I import it I get no problem, but when I want to do a simple animation I get the following error:

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')

Here is my code, which is a simple Loading cicrle:

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;

I import this in App.js:

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

And here is my 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

Answers


  1. you get this error because you have been using a hook outside of a functional component just change it inside and the error will be fixed

    Login or Signup to reply.
  2. One of the causes of this problem is different versions of React installed. I got the same issue and solved like this.

    If you are using npm, run this.

    npm install --legacy-peer-deps

    Then start the project with expo start -c

    If you are using yarn, add this to your package.json and start the project again:

      "resolutions": {
        "react": "your-version-here"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search