对象作为 React 子对象无效

新手上路,请多包涵

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

对象作为 React 子级无效。如果您打算渲染一组子对象,请改用数组或使用 React 附加组件中的 createFragment(object) 包装对象。检查 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>
);

}

原文由 Mesmerize86 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 529
1 个回答

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>

原文由 Emilio Rodriguez 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题