1

I have this state which is nested and I am having difficulty in setting state. Error:undefined is not an object (evaluating this.state.form) My initial state is :

this.state={
    activePage:0,
    visible:false,
    questionType:null,
    form:{
        title:'',
        description:'',
        pages:[{
            title:'',
            description:'',
            questions:[

            ]
        }]
    }
};

Now every time a user clicks, I need to add more object to pages array which should be having a title (''), description ('') and questions (empty list). I tried achieving this but it doesn't seem to be working.

let newForm={
     ...this.state,
     form:{
          ...this.state.form,
          pages:[
               ...this.state.form.pages,
               pageData
          ]
     }
 };
 console.log('newForm',newForm);
 this.setState({
     form:newForm
 });

This is how my pageData looks like

let pageData={
    title:'',
    description:'',
    questions:[]
};
2
  • 1
    "Doesn't seem to be working" - what's the specific issue? Commented Feb 8, 2019 at 5:24
  • undefined is not an obect (evaluating this.state.form) Commented Feb 8, 2019 at 5:26

2 Answers 2

1

You are setting the entire state in newForm. Just limit it to the form object:

let newForm = {
  ...this.state.form,
  pages: [
    ...this.state.form.pages,
    pageData
  ]
};

console.log('newForm', newForm);

this.setState({
  form: newForm
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi adiga are you interested in small JavaScript consulting jobs? More details: myhotpot.io/jobs. For instance ,we would like to outsource a parser for extracting gradients from background-images. Please say when you have read this, and so this comment can get deleted. Thanks!
1

Whenever you see yourself in a situation where your state is getting complex or nested it means you should be rethinking you component structure, I mean you should be bringing in more granular components to manage their own state.

so why not move form object in your state to a different component completely.

I recommend creating a separate component for form object and let the form component manage the form state.

import React, { Component } from 'react';

class form extends Component {
    state = {
      form:{
            title:'',
            description:'',
            pages:[{
                title:'',
                description:'',
                questions:[

                ]
            }]
        }
    }

    render() {
    //Form content
    }
}

export default form;

more granular your components are more easy to manage and that's exactly the way you should be doing things in React.

1 Comment

This is really a comment, not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.