本文介绍了对象作为 React 子对象无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到以下错误.我可以看到我必须返回数组而不是对象.但我真的不知道如何解决它.提前致谢
I am getting an below error. I can see that I have to return array instead of object. But I am really not sure how to fix it. Thanks in advance
对象作为 React 子对象无效.如果你想渲染一个孩子的集合,使用数组代替或使用包装对象来自 React 插件的 createFragment(object).检查渲染方法查看
.
constructor(props){
super(props);
this.state = {timeElapsed: null};
}
startStopButton(){
return <TouchableHighlight underlayColor="gray" onPress={this.handleStartPress.bind(this)}>
<Text>Start</Text>
</TouchableHighlight>
}
handleStartPress(){
var startTime = new Date();
setInterval(()=>{
this.setState({timeElapsed: new Date()})
}, 1000);
}
render(){
return(
<View style={styles.container}>
<View style={[styles.header]}>
<View style={[styles.timerContainer, this.borderColor('#ff6666')]}>
{this.state.timeElapsed}
</View>
<View style={[styles.buttonsContainer, this.borderColor('#558000')]}>
{this.startStopButton()}
{this.lapButton()}
</View>
</View>
</View>
);
}
推荐答案
timeElapsed 是一个对象,React 不知道如何渲染:
timeElapsed is an object, React doesn't know how to render this:
<View style={[styles.timerContainer, this.borderColor('#ff6666')]}>
{this.state.timeElapsed}
</View>
尝试将 this.state.timeElapsed
更改为字符串,例如:
Try changing this.state.timeElapsed
for a string like for example:
<View style={[styles.timerContainer, this.borderColor('#ff6666')]}>
{this.state.timeElapsed.toString()}
</View>
这篇关于对象作为 React 子对象无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!