【发布时间】:2018-09-05 18:29:30
【问题描述】:
我正在尝试实现我的渲染函数,以便它等待直到我完成从网络上抓取数据(如果 AsyncStorage 没有它)或从 AsyncStorage 获取它,但我正在努力解决承诺方面的问题它:
render() {
let pic = {
uri: 'https://i.imgur.com/G19Jnuj.jpg'
};
this.renderAttributes()
.then((response) => {
console.log("finished val: " + this.state.name);
console.log("finished response: " + response);
return (
<View style={shared.basicView}>
<Image source={pic} style ={styles.circleImage}/>
<Text style={styles.infoText}>{this.state.name}</Text>
<Text style={styles.titleText}>Bio</Text>
<Text style={styles.infoText}>blah blah</Text>
<Text style={styles.titleText}>Email</Text>
<Text style={styles.infoText}>blah blah</Text>
<Text style={styles.titleText}
onPress={() => this.props.navigation.navigate('Repositories')}>
Public Repos Count</Text>
<Text style={styles.infoText}>3</Text>
<Text style={styles.titleText}
onPress={() => this.props.navigation.navigate('Followers')}>
Follower Count</Text>
<Text style={styles.infoText}>0</Text>
<Text style={styles.titleText}
onPress={() => this.props.navigation.navigate('Following')}>
Following Count</Text>
<Text style={styles.infoText}>0</Text>
<Text style={styles.titleText}>Profile Created</Text>
<Text style={styles.infoText}>November 2015</Text>
</View>
)
})
.catch((error) => {
console.log("Profile creation failed: " + error);
return(null);
})
}
类的其余部分和 renderAttributes 函数的定义如下:
class ProfileScreen extends Component {
state: any;
constructor(props) {
super(props);
this.state = {
name: '',
bio: '',
email: '',
repoCount: '',
followerCount: '',
followingCount: '',
profileCreated: ''
};
this.renderAttributes = this.renderAttributes.bind(this);
}
async renderAttributes() {
console.log("inside ga");
let jsonData=null;
try {
axios.get('https://api.github.com/users/dummy')
.then(async (response) => {
console.log("Response: " + JSON.stringify(response));
jsonData=response;
console.log("jsonData: " + JSON.stringify(jsonData));
const nameVal = await AsyncStorage.getItem('name');
if(nameVal !== null) {
console.log("name val: " + nameVal);
this.setState({name: nameVal});
}
else {
try {
await AsyncStorage.setItem('name', jsonData.data.name);
}
catch (error) {
console.log("Set name error: " + error);
}
if(jsonData.data.name == null) {
this.setState({name: 'n/a'});
}
else {
console.log("name: " + jsonData.data.name);
this.setState({name: jsonData.data.name});
}
}
const bio = await AsyncStorage.getItem('bio');
if(bio !== null) {
this.setState({bio: bio});
}
else {
try {
await AsyncStorage.setItem('bio', jsonData.data.bio);
}
catch (error) {
console.log(error);
}
if(jsonData.data.bio == null) {
this.setState({bio: 'n/a'});
}
else {
this.setState({bio: jsonData.data.bio});
}
}
// etc for each attribute in state
})
.catch((error) => {
console.log('Error fetching: ' + error);
});
}
catch (error) {
console.log('Error creating attributes: ' + error);
}
}
(请原谅我所有的 console.log 语句)。对我来说,无论如何这似乎都应该返回 null 或返回 View,但我不断收到错误 "Invariant Violation: ProfileScreen(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null."。我的 catch 语句都没有被触发。老实说,我很困惑;尽管这可能是罪魁祸首,但我注意到了两件事:1. 我的 IDE 将 this.state.name 突出显示为未解析的变量名,2. 我没有在 renderAttributes 中明确定义承诺(我是否必须?)。此外,仅基于打印语句,似乎this.renderAttributes().then(response) 中的内容在axios.get.then() 之前打印;不确定这是否是一个红鲱鱼。有什么建议吗?
【问题讨论】:
-
我不是 React 用户,但从我上次玩它的时候开始,
render不是async操作,要处理async你会改变state,这个然后会导致render方法,它来自您将更新视图的状态。 -
@Keith 嗯,我想这就是我认为我在做的事情。这绝对是一个菜鸟问题,但是为什么渲染不是异步的很重要呢?如果我强迫它等待
renderAttributes(),那还不够吗? -
I guess that's what I thought I was doing,一旦你使用 Promises、async / await / callbacks 等,你就使用 async.. ps。当你await某些东西时,它不会成为sync方法。
标签: javascript react-native promise render