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

0

1 Answer 1

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

This should update correctly

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, i forgot the spread operator... :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.