Invalid hook call. Hooks can only be called inside of the body of a function component
Пишу авторизацию через ВК на React Native. Когда нажимаю на кнопку "Войти с помощью ВКонтакте" ловлю ошибку.

Сама по себе функциональность работает, данные пользователя от ВК я получаю, но я хочу это делать именно при нажатии на кнопку авторизации, а с кнопкой что-то не то.
Мой код:
import React, {useState, useEffect} from 'react';
import {StyleSheet, Text, View, TouchableOpacity} from 'react-native';
import * as AuthSession from 'expo-auth-session';
const App = () => {
const [hasError, setErrors] = useState(false);
const [userData, setState] = useState({});
useEffect(() => {
async function fetchData(e) {
e.preventDefault()
let result = await AuthSession.startAsync({
authUrl: 'https://oauth.vk.com/authorize?client_id=7563861&display=mobile&redirect_uri=https://auth.expo.io/@laneboyandrew/beautifulPlaces&response_type=token&v=5.92',
});
if (result.type === 'success') {
const res = await fetch('https://api.vk.com/method/users.get?v=5.92&access_token=' + result.params.access_token);
res
.json()
.then(res => setState(res))
.catch(err => setErrors(err));
}
}
fetchData();
});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#e9ebee',
alignItems: 'center',
justifyContent: 'center',
},
loginBtn: {
backgroundColor: '#4267b2',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 20
},
logoutBtn: {
backgroundColor: 'grey',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 20,
position: "absolute",
bottom: 0
},
});
return (
<View style={styles.container}>
<TouchableOpacity style={styles.loginBtn} onPress={App}>
<Text style={{color: "#fff"}}>Войти с помощью ВКонтакте</Text>
</TouchableOpacity>
</View>
);
};
export default App;
Ответы (2 шт):
Автор решения: Andrey Grach
→ Ссылка
Так всё работает.
import React, {useState, useEffect} from 'react';
import {Button, StyleSheet, View} from "react-native";
import * as AuthSession from "expo-auth-session";
import Location from "./Location";
const VkAuthorize = (props) => {
const [hasError, setErrors] = useState(false);
const [userData, setState] = useState({});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#e9ebee',
alignItems: 'center',
justifyContent: 'center',
},
loginBtn: {
backgroundColor: '#4267b2',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 20
},
logoutBtn: {
backgroundColor: 'grey',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 20,
position: "absolute",
bottom: 0
},
});
return (
<View style={styles.container}>
<Button style={styles.loginBtn} onPress={
async function fetchData(e) {
e.preventDefault()
let result = await AuthSession.startAsync({
authUrl: 'https://oauth.vk.com/authorize?client_id=7563861&display=mobile&redirect_uri=https://auth.expo.io/@laneboyandrew/beautifulPlaces&response_type=token&v=5.92',
});
console.log(result)
if (result.type === 'success') {
const res = await fetch('https://api.vk.com/method/users.get?v=5.92&access_token=' + result.params.access_token);
res
.json()
.then(res => props.setState(res))
.catch(err => props.setErrors(err));
}
}
} title="Войти через ВКонтакте"/>
</View>
);
}
export default VkAuthorize