this.setState不适用于我的React本机项目

时间:2019-02-01 13:59:54

标签: reactjs react-native setstate

在我正在开发this.setState的我的本机应用程序上,setState不起作用,并且不更新构造函数中的值。但是this.state({})正在工作。但是当我使用它正在重置构造函数的时候,任何人请指出这段代码有什么问题

import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, TextInput, Image } from 'react-native';
import { RkTextInput, RkButton } from 'react-native-ui-kitten';
import * as firebase from 'firebase';

export default class Home extends React.Component {
    constructor(props) {
        super(props)

        this.state = {
            uType: 'normal',
            AdminNavbutton: ''
        }
    }

    ProfileSubmithandler = () => {
        alert(this.state.uType + " " + this.state.AdminNavbutton)
    }

    componentDidMount() {
        this.readUserData();
    }

    readUserData = () => {
        userstype = 'users/' + firebase.auth().currentUser.uid + '/userType'

        firebase.database().ref(userstype).once('value').then(snapshot => {
            this.setState({ uType: snapshot.val() }) // notworking and not updating uType
            alert(snapshot.val()) //working Properly
        })

    }

    emailid = () => {

    }

    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.titleText}>Taams</Text>
                <Text style={styles.edition}>Developer's Edition</Text>
                <Text style={styles.titleText}>Home</Text>
                <Text>Alpha 0.0.0.1</Text>
                <RkButton onPress={() => this.ProfileSubmithandler()}>
                    <Text style={styles.LoginButtonText}>Submit</Text>
                </RkButton>
                <TouchableOpacity onPress={() => this.props.navigation.navigate(this.state.AdminNavbutton)}><Text style={styles.signinButton}>Profile</Text></TouchableOpacity>
            </View>
        );
    }
}

3 个答案:

答案 0 :(得分:1)

this.setState()是异步的,调用后立即可以使用您的更新值。

完成setState()的值更新后,您将可以在render下的this.state.uType函数中访问更新的数据。

如果您想立即在函数中使用值,则应将其放在另一个变量中:

readUserData = () => {
    userstype = 'users/' + firebase.auth().currentUser.uid + '/userType'

    firebase.database().ref(userstype).once('value').then(snapshot => {
        const uType = snapshot.val()
        this.setState({ uType }) // notworking and not updating uType
        alert(snapshot.val()) //working Properly
        alert(uType) // Also working
    })

}

答案 1 :(得分:1)

您忘记为“ userstype”声明变量-第25行

答案 2 :(得分:0)

这是由于setSate中的异步问题。当设置从Firebase和状态提取的数据时,让该按钮显示。您在这里:

           import React from 'react';
                    import { StyleSheet, Text, View, TouchableOpacity, TextInput, Image } from 'react-native';
                    import {RkTextInput, RkButton } from 'react-native-ui-kitten';
                    import * as firebase from 'firebase';



                    export default class Home extends React.Component {
                      constructor(props){
                        super(props)

                        this.state={
                          uType:'normal',
                          AdminNavbutton:false,
                        }
                      }

                      ProfileSubmithandler=()=>{
                        alert(this.state.uType + " " + this.state.AdminNavbutton)


                      }
                      componentDidMount() {
                        this.readUserData();
                      };

                      readUserData=()=> {
                        userstype= 'users/'+ firebase.auth().currentUser.uid + '/userType'

                        firebase.database().ref(userstype).once('value').then(snapshot => {
                            this.setState({ uType: snapshot.val() }) // notworking and not updating uType
                            alert(snapshot.val()) //working Properly
                        })

                        }
                        emailid=()=>{
                        }


                      render() {
                        return (

                          <View style={styles.container}>
                            <Text style={styles.titleText}>Taams</Text>
                            <Text style={styles.edition}>Developer's Edition</Text>
                            <Text style={styles.titleText}>Home</Text>
                            <Text>Alpha 0.0.0.1</Text>
                         {
                          this.state.AdminNavbutton &&
                            <RkButton onPress = {()=>this.ProfileSubmithandler()}>
                              <Text style={styles.LoginButtonText}>Submit</Text>
                            </RkButton>



                        <TouchableOpacity onPress={()=>this.props.navigation.navigate(this.state.AdminNavbutton)}><Text style={styles.signinButton}>Profile</Text></TouchableOpacity>
                        }
                          </View>
                        );
                      }
                    }