在状态转换错误期间无法更新-导航到另一个屏幕时使用挂钩对本机做出反应

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在状态转换错误期间无法更新-导航到另一个屏幕时使用挂钩对本机做出反应相关的知识,希望对你有一定的参考价值。

您好,我一直在尝试解决所有可能的方法,但仍然找不到问题,只有在应用程序发现用户已经第一次看到该屏幕之后,该应用程序导航到其他屏幕时才会出现问题,我当时想是因为即时通讯离开时状态仍未更新?任何帮助表示赞赏。

function UserScreen(navigation) 




    useEffect(() =>         
        checkIfFisrtTime()    
, [])


    const [name, setname] = useState(undefined)
    const [altura, setaltura] = useState(undefined)
    const [peso, setpeso] = useState(undefined)
    const [hombre, sethombre] = useState(false)
    const [mujer, setmujer] = useState(false)
    const [error, seterror] = useState(undefined)



    const [loading, setloading] = useState(true)
    const [first, setfirst] = useState()


    async function checkIfFisrtTime() 


            const value = await AsyncStorage.getItem("firstLogging")
             setfirst(value)
            setloading(false)


    


    function check() 
        console.log(first)
        console.log(loading)
    


if (loading==true && first ==undefined || first==null && loading===true || first=="false" && loading===true || first===undefined && loading ===false)  


    return (<ActivityIndicator></ActivityIndicator>)




if(first==="false" && loading === false )   


    return navigation.navigate("Mainflow")



if (  first==null && loading===false ) 



    return (



        <View style= top: height * 0.1, flexDirection: "column", justifyContent: "space-between", height: height * 0.6 >



            <Text style= textAlign: "center", fontWeight: "bold" >i18n.t("Information")</Text>
            <CheckBox

                left
                title=i18n.t("Male")
                checkedIcon='dot-circle-o'
                uncheckedIcon='circle-o'

                checked=hombre
                onPress=() =>  sethombre(true), setmujer(false) 
                containerStyle= width: width * 0.5, alignSelf: "center", borderRadius: 15 
                icon
            />

            <CheckBox
                left
                title=i18n.t("Female")
                checkedIcon='dot-circle-o'
                uncheckedIcon='circle-o'
                checked=mujer
                onPress=() =>  setmujer(true), sethombre(false) 
                containerStyle= width: width * 0.5, alignSelf: "center", borderRadius: 15 
            />




            <Input
                placeholder=i18n.t("Age")
                errorStyle= color: 'red' 
                errorMessage=undefined
                onChangeText=(name) =>  setname(name.replace(/[- #*;,.<>\\\[\]\\\/]/gi, '')) 
                value=name
                keyboardType="numeric"
                maxLength=2
                containerStyle= width: width * 0.5, alignSelf: "center" 
                leftIcon=
                    <MaterialCommunityIcons name="vanish" size=24></MaterialCommunityIcons>
                
            />

            <Input
                placeholder=i18n.t("Height")
                errorStyle= color: 'red' 
                errorMessage=undefined
                onChangeText=(altura) =>  setaltura(altura.replace(/[- #*;,.<>\\\[\]\\\/]/gi, '')) 
                value=altura
                keyboardType="numeric"
                maxLength=3
                containerStyle= width: width * 0.5, alignSelf: "center" 
                leftIcon=
                    <MaterialCommunityIcons name="arrow-split-horizontal" size=24></MaterialCommunityIcons>
                
            />
            <Input
                placeholder=i18n.t("Weight")
                errorStyle= color: 'red' 

                onChangeText=(namee) =>  setpeso(namee.replace(/[- #*;,.<>\\\[\]\\\/]/gi, '')) 
                value=peso
                keyboardType="numeric"
                maxLength=3
                containerStyle= width: width * 0.5, alignSelf: "center" 
                leftIcon=
                    <MaterialCommunityIcons name="scale-bathroom" size=24></MaterialCommunityIcons>
                
                errorMessage=error
            />


            <Button title=i18n.t("Continue") containerStyle= width: width * 0.4, alignSelf: "center"  raised onPress=()=>ValidateIfundefined()>

            </Button>

            <Button title="boton" onPress=()=>check()></Button>



        </View>


    )

    async function ValidateIfundefined()    

        if (name==undefined && altura==undefined && peso==undefined && hombre==false && mujer==false) 


            //seterror("Please make sure everything is ok")
        

        if (altura==undefined || peso==undefined || altura==undefined || hombre==false && mujer == false) 


            //seterror("Please fill everything")
        

        if (name!=undefined && altura!=undefined && peso!=undefined && mujer!=false && hombre==false) 

            await AsyncStorage.setItem("firstLogging","false")
            navigation.navigate("Mainflow")

        

        if (name!=undefined && altura!=undefined && peso!=undefined && hombre!=false && mujer==false) 
            await AsyncStorage.setItem("firstLogging","false")       
            navigation.navigate("Mainflow")
        

    








答案

尝试此操作(示例代码),可能是堆栈导航器问题

componentDidMount() 
     const  navigation  = this.props;
     navigation.addListener('willFocus', () => 
         // this.getListData(); fetch data here
     );
     // this.setState( listData: data );

以上是关于在状态转换错误期间无法更新-导航到另一个屏幕时使用挂钩对本机做出反应的主要内容,如果未能解决你的问题,请参考以下文章

根据 Firebase 状态导航到另一个屏幕

相机不显示,状态转换问题,反应原生

iOS 应用在导航期间迁移到 swift3 后崩溃

Flutter:从 StreamBuilder 构建器回调内部导航到另一个屏幕

导航返回时不清除文本输入并验证错误消息

listView不显示,除非我滚动它