Problem when trying to call a function inside onPress - React Native

Asked

Viewed 203 times

0

Hello, When trying to call a function that has a hook call the following error appears:inserir a descrição da imagem aqui

And here’s the excerpt from the code:

currentLocation.js

import React, {useState} from 'react'

export function locationUser(){
  ...
  const [coordinates, setCoordinates] = useState({
    latitude:0,
    longitude: 0,
    error: null
  });
  ...
  return console.log(coordinates); 
}

index js.

import React from 'react';
import { View, Button, Text } from 'react-native';
import { locationUser } from '../../services/currentLocation'

export default function Home(){

    return(
      <View>
        ...
        <Button onPress={() => locationUser()}>
          <Text>INICIAR</Text>
        </Button>
        ...
      </View>
  );
}

if you try to execute locationUser without is inside the fuction Arrow works, however, is not right, because the function will run infinitely, since a normal function runs as soon as the code is started.

Can someone help me? =)

1 answer

0

  • More in my case I’m using Function Component and not Class Component, in which part do you think I’m mixing?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.