1

im quite stuck here i have a state like this

state={
panelData:[ 
             { title:"title"
               content:["one",2] 
             }
          ]         
       }

now in my setState i want to add new title and content, also have my previous added data too , so two objects right? like this

panelData: [     {title:"title",content:["one",2]} ,
                 {title:"title 2"content:[3,4]}     ]

i tried this

    handlingAddRecipe(){ //this is my update paneldata function
        let contentValue = this.state.recipeContent.join("").split(","); //contentValue = ["content1","content2"]

        this.setState({
          panelData:[{
            title:this.state.recipeTitle,   //title:"title1"
            content:contentValue            //content:contentValue  
          }]
        })
        console.log(this.state.panelData)
  }

im getting the output which has no previous data only the data which is passed in to paneldata

Pollux 01
  • 121
  • 3
  • 11

1 Answers1

2
this.setState({
          panelData:[...this.state.panelData,{
            title:this.state.recipeTitle,   //title:"title1"
            content:contentValue            //content:contentValue  
          }]
        })

This should update correctly

simbathesailor
  • 3,681
  • 2
  • 19
  • 30