我收到以下错误.我可以看到我必须返回数组而不是对象.但我真的不确定如何解决它.提前致谢
Objects are not valid as a React child. If you meant to render a
collection of children,use an array instead or wrap the object using
createFragment(object) from the React add-ons. Check the render method
ofView
.
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>