Objects are not valid as a React child

前端 未结 3 467
轻奢々
轻奢々 2020-12-03 02:26

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

Object

相关标签:
3条回答
  • 2020-12-03 02:49
    <Note note={
     <p>{(new Date(updated_at)).toString()}</p>
    } />
    

    In this case, Note is my child component and I had passed date as above and it worked for me.

    Output Pass date to React Child

    0 讨论(0)
  • 2020-12-03 02:50
    • Short & simple is that use JSON.stringify()
    • because this.state.timeElapsed is the object. object not able to print in normal standered. Thus we need to render that object in JSON.stringify(). It converted into JSON form.

    • Try this, It's work for me

    {JSON.stringify(this.state.timeElapsed)}
    
    0 讨论(0)
  • 2020-12-03 03:00

    timeElapsed is an object, React doesn't know how to render this:

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

    Try changing this.state.timeElapsed for a string like for example:

      <View style={[styles.timerContainer, this.borderColor('#ff6666')]}>
        {this.state.timeElapsed.toString()}
      </View>
    
    0 讨论(0)
提交回复
热议问题