Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to resolve this Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component

I think i miss something.

I want to navigate to another screen. I am getting this As error

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component

Checking Thru, I am not sure as to what the challenge is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code Looks thus :

import {
  Text,
  View,
  TextInput,
  ImageBackground,
  StyleSheet,
  Dimensions,
  TouchableOpacity,
  Image,
} from 'react-native';
import React from 'react';
import image from '../../assets/images/1.png';
import btnimage from '../../assets/images/button1.png';
import {useNavigation} from '@react-navigation/native';

const screenHeight = Dimensions.get('window').height;
const screenWidth = Dimensions.get('window').width;

const navigation = useNavigation();

const NavigateNextPage = () => {
  navigation.navigate('WalkThruPage2');
};

const PlayVideo = () => {
  return (
    <View>
      <ImageBackground source={image} resizeMode="stretch" style={styles.img} />
      <TouchableOpacity onPress={() => { NavigateNextPage();}}
        style={{position: 'absolute', bottom: 20, right: 30, zIndex: 1}}>
        <Image style={{height: 60, width: 60}} source={btnimage} />
      </TouchableOpacity>
    </View>
  );
};

export default PlayVideo;

const styles = StyleSheet.create({
  img: {
    height: screenHeight,
    width: screenWidth,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

What Am i getting wrong? I want it to be that, once they click the Touchable Opacity it loads to next page. But on Loading I get that Error. How Do i resolve this?

>Solution :

As error suggests hooks can’t be called outside of function so modify your code as below:

const PlayVideo = () => {

const screenHeight = Dimensions.get('window').height;
const screenWidth = Dimensions.get('window').width;

const navigation = useNavigation();

const NavigateNextPage = () => {
  navigation.navigate('WalkThruPage2');
};


 return (
    <View>
      <ImageBackground source={image} resizeMode="stretch" style={styles.img} />
         <TouchableOpacity onPress={() => { NavigateNextPage();}}
          style={{position: 'absolute', bottom: 20, right: 30, zIndex: 1}}>
           <Image style={{height: 60, width: 60}} source={btnimage} />
         </TouchableOpacity>
    </View>
 );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading