0

I want to update question name on onChange event. But problem is every question changes if I change only one ques.How to fix it ?

class QuestionCreate extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            datas: [],
            default_question: {
                isNew: true,
                question: {
                    name: 'Click to write the question text',
                    answer_options: [
                        {name: 'click to write choice 1', 'choice': true},
                        {name: 'click to write choice 1', 'choice': true},
                        {name: 'click to write choice 1', 'choice': true},
                    ]
                }
            }
        }

    }


    onChangeQuestion(qustions, index, event){
        let all_data = this.state.datas;
        let currentQuestion = all_data[index].question
        all_data[index]['question']['name'] = event.target.value;
        console.log(all_data[index]['question']['name'])
        this.setState({datas: all_data})

    }

    displayRow(){
    let rowData = this.state.datas.map( (data, index) =>{
        console.log("this is data:", data, index);
        return(
            <div className="well" key={ index }>
                <h3>Q. <input type="text" onChange={ this.onChangeQuestion.bind(this, data, index) } value={ data.question.name } /></h3>
                    <ul>
                        <li>option</li>
                    </ul>
            </div>
        )
    })

    return rowData;
}

enter image description here

3
  • Can you put this code in Fiddle or Codepen or Plunker? Commented Oct 24, 2017 at 4:49
  • Please post your initial data and what data you are getting after operation. Commented Oct 24, 2017 at 4:51
  • jsfiddle.net/584x2qg1. I have put it in js fiddle Commented Oct 24, 2017 at 5:01

2 Answers 2

2

You are mutating your state directly.

let all_data = this.state.datas;
let currentQuestion = all_data[index].question
all_data[index]['question']['name'] = event.target.value;

Use:

let all_data = JSON.parse(JSON.stringify(this.state.datas));
let currentQuestion = all_data[index].question;
all_data[index]['question']['name'] = event.target.value;
this.setState({datas: all_data});

These questions may also be helpful.

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

10 Comments

You are changing this.state by using the above statements. This is not good. You can read reactjs.org/tutorial/… to better understand the issue.
Do ask if you still have any doubts
onChangeQuestion(qustions, index, event){ let all_data = this.state.datas; let currentQuestion = all_data[index].question let newQues = Object.assign({}, currentQuestion, {name:event.target.value}) this.setState({ datas: all_data }) }
is there need this line ? this.setState({ datas: all_data })
in the line let all_data = this.state.datas;, you are assigning a reference to this.state.datas. So when you change all_data, you change this.state. In order to avoid that, you can do let all_data = JSON.parse(JSON.stringify(this.state.datas)); to create a copy of this.state.datas.
|
0

it is working after editing onChangeQuestion function.

onChangeQuestion(qustions, index, event){
    let all_data = JSON.parse(JSON.stringify(this.state.datas)); 
    let currentQuestion = all_data[index].question 
    all_data[index]['question']['name'] = event.target.value; 
    this.setState({datas: all_data});
}

Comments

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.