gpt4 book ai didi

react-native - 对象作为 React 子对象无效

转载 作者:行者123 更新时间:2023-12-03 07:11:27 25 4
gpt4 key购买 nike

我收到以下错误。我可以看到我必须返回数组而不是对象。但我真的不知道如何解决它。

Objects are not valid as a React child. If you meant to render acollection of children, use an array instead or wrap the object usingcreateFragment(object) from the React add-ons. Check the render methodof View.

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 不知道如何渲染它:

  <View style={[styles.timerContainer, this.borderColor('#ff6666')]}>
{this.state.timeElapsed}
</View>

尝试更改 this.state.timeElapsed 为字符串,例如:

  <View style={[styles.timerContainer, this.borderColor('#ff6666')]}>
{this.state.timeElapsed.toString()}
</View>

关于react-native - 对象作为 React 子对象无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41604539/

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