Add element to a state React

后端 未结 2 1747
误落风尘
误落风尘 2020-12-04 00:55

I already have a state with this:

 this.setState({
      conversation:
          (
            
{conversation.map(element => {
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 01:24

    I don't think it's a good idea to store jsx components in the state of a component. I think you should only save the data in the state needed to render the component.

    If you really want to store jsx in the state, why won't you define your 'conversation' property as array? Then you'll be able to add new components to it.

    this.setState({
      conversation: [
            (
    firstnew div
    )); this.setState({conversation: currentConversation})

    But better to only store the data ie 'first' and 'new div'

提交回复
热议问题