gpt4 book ai didi

javascript - ReactNative : Cannot update during an existing state transition (such as within `render` ). 渲染方法应该是 Prop 和状态的纯函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:31 26 4
gpt4 key购买 nike

这是我的代码,我似乎找不到问题所在。我已经研究了这个问题,但找不到解决方案,所以我向 StackOverflow 大神寻求帮助!

如您所见,我想在我的 LoadingScreen 中加载字体,并在完成后移至下一个屏幕。如果有更简单的方法,请告诉我。

import React from 'react';
import { StyleSheet, View, AsyncStorage, Alert, ActivityIndicator } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient'
import * as Font from 'expo-font';
import * as firebase from "firebase";


export default class Loading extends React.Component {

constructor(props) {
super(props);
this.state = {
fontLoaded: false,
client: {
uid: ""
}
};
}


async componentWillMount() {
//Load fonts + Login to Firebase + capture user ID
let self = this;
await Font.loadAsync({
"Roboto-Regular": require("../assets/fonts/Roboto-Regular.ttf"),
"Courgette-Regular": require("../assets/fonts/Courgette-Regular.ttf"),
"Raleway-Regular": require("../assets/fonts/Raleway-Regular.ttf")


})

await firebase.auth().signInAnonymously().catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
Alert.alert("Error code : " + errorCode, errorMessage)

});

//Register the UID
await this.setState({ uid: firebase.auth().currentUser.uid })

this.setState({
client: {
...this.state.client,
uid: firebase.auth().currentUser.uid
}
});




await this.setState({ fontLoaded: true })



}


render() {

if (this.state.fontLoaded) {

return (

this.props.navigation.navigate("Home", { client: this.state.client })
)
}

return (
<View style={styles.container}>
<LinearGradient
colors={["#5B86E5", "#36D1DC"]}
style={{ flex: 1 }}
>
<View style={{ justifyContent: "center", alignItems: "center", flex: 1 }}>

<ActivityIndicator size="large" color="#FFF" />

</View>
</LinearGradient>
</View >
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1
}


});

最佳答案

我认为加载您的 Home 屏幕会更好。执行诸如在页面中获取数据并在操作完成后导航到另一个页面的操作效率不高。我认为最好在 componentDidMount 生命周期中获取您的数据,并且当收到数据时将您的 fontLoaded 更改为 true ,如下所示:

import React from 'react';
import { StyleSheet, View, AsyncStorage, Alert, ActivityIndicator } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient'
import * as Font from 'expo-font';
import * as firebase from "firebase";


export default class Home extends React.Component {

constructor(props) {
super(props);
this.state = {
fontLoaded: false,
client: {
uid: ""
}
};
}


async componentDidMount() {
//Load fonts + Login to Firebase + capture user ID
let self = this;
await Font.loadAsync({
"Roboto-Regular": require("../assets/fonts/Roboto-Regular.ttf"),
"Courgette-Regular": require("../assets/fonts/Courgette-Regular.ttf"),
"Raleway-Regular": require("../assets/fonts/Raleway-Regular.ttf")


})

await firebase.auth().signInAnonymously().catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
Alert.alert("Error code : " + errorCode, errorMessage)

});

//Register the UID
await this.setState({ uid: firebase.auth().currentUser.uid })

this.setState({
client: {
...this.state.client,
uid: firebase.auth().currentUser.uid
}
});




await this.setState({ fontLoaded: true })



}


render() {

if (this.state.fontLoaded) {

return (
........... Any code that presents in your Home component

)
}

return (
<View style={styles.container}>
<LinearGradient
colors={["#5B86E5", "#36D1DC"]}
style={{ flex: 1 }}
>
<View style={{ justifyContent: "center", alignItems: "center", flex: 1 }}>

<ActivityIndicator size="large" color="#FFF" />

</View>
</LinearGradient>
</View >
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1
}


});

希望对您有所帮助。如果这是您想要的解决方案,请给我投票:)

关于javascript - ReactNative : Cannot update during an existing state transition (such as within `render` ). 渲染方法应该是 Prop 和状态的纯函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57748035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com