this.setState不适用于我的React原生项目



在我正在开发的react本机应用程序上,setState不工作,也不更新构造函数中的值。但是这个.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>
);
}
}

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

setState()更新完您的值后,您将能够访问this.state.uTyperender函数中的更新数据。

如果你想在函数中立即使用你的值,你应该把它放在另一个变量中:

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
})
}

您忘记为"usertype"声明一个变量-第25行

这是因为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>
);
}
}

相关内容

  • 没有找到相关文章

最新更新