亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

掛鉤調用無效。鉤子只能在函數組件的主體內部調用。如何解決這個問題?

掛鉤調用無效。鉤子只能在函數組件的主體內部調用。如何解決這個問題?

四季花海 2023-03-03 19:30:34
***我已經制作了自定義的 AuthContext,它為 Singnin 和 SignOut 頁面進行 API 調用,基本上 AuthContext 中有函數,我想在下面的類組件中調用它們所以會看到我已經在類組件內部使用這段代碼對 singOutFunction 進行了解構,代碼如下 const {state, signOutFunction, clearMessage}=useContext(AuthContext)請告訴我我做錯了什么,并告訴我在類組件中在哪里解構我的 signOutFunctionimport React,{useContext} from 'react';import {View, StyleSheet, ScrollView, ToastAndroid, Alert} from 'react-native';import AsyncStorage from '@react-native-community/async-storage';import ProfileTab from './ProfileTab';import {BackHeader} from '../components/Headers';import {RoundButtonArray, SignOutBtn} from '../components/Buttons';import {btnArray} from '../helpers/MapInputs';import FlatButton from '../components/FlatButton'import Spacer from '../components/Spacer';//////////////////////////////////////////////////////////////////////////////////**import {Context as AuthContext} from '../context/AuthContext'**const dummyText = {  name: 'Dhruva H',  email: '[email protected]',  prep: 'CET',};class Profile extends React.Component {const {state, signOutFunction, clearMessage}=useContext(AuthContext)    //   signOutPress = async () => {//     await AsyncStorage.clear();//     this.props.navigation.navigate('LoadStack');//     ToastAndroid.show('Signed Out!', ToastAndroid.SHORT);//   };  onSignOut = async () => {           Alert.alert(      'Sign out',      'Are you sure you want to Sign out?',      [        {          text: 'Cancel',          onPress: () => null,          style: 'cancel',        },        {text: 'OK', onPress: signOutFunction()},      ],      {cancelable: true},    );  };  onImagePress = () => {    ToastAndroid.show('Hi', ToastAndroid.SHORT);  };  render() {       return (      <View style={styles.container}>        <BackHeader          route="Home"          title="PROFILE"          type="row"          backIcon="ios-arrow-dropright"        />
查看完整描述

1 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

就像評論中提到的那樣,您將不得不將基于類的組件轉換為功能組件,


function Profile() {

    const { state, signOutFunction, clearMessage } = useContext(AuthContext);


    const onSignOut = async () => {

        Alert.alert(

            'Sign out',

            'Are you sure you want to Sign out?',

            [

                {

                    text: 'Cancel',

                    onPress: () => null,

                    style: 'cancel',

                },

                { text: 'OK', onPress: signOutFunction() },

            ],

            { cancelable: true }

        );

    };


    const onImagePress = () => {

        ToastAndroid.show('Hi', ToastAndroid.SHORT);

    };


    return (

        <View style={styles.container}>

            <BackHeader route="Home" title="PROFILE" type="row" backIcon="ios-arrow-dropright" />

            <ScrollView>

                <ProfileTab data={dummyText} imagePress={onImagePress} />

                <RoundButtonArray btnArray={btnArray} />

                <Spacer />

                <FlatButton name="Log Out" onClick={onSignOut} />

            </ScrollView>

        </View>

    );

};


查看完整回答
反對 回復 2023-03-03
  • 1 回答
  • 0 關注
  • 84 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號