3
  constructor(props) {
   super(props);
    this.state = {
     answers: props.element.answers_data.map((answer, index) => {
     return answer;
    }
  })

     <TextInput
      value = {this.state.answers[index].answer_text}
     onChangeText={(answer_text) => {
              this.setState({
                answers: [
                    ...this.state.answers.slice(0, index),
                    {answer: {answer_text}},
                    ...this.state.answers.slice(index + 1, 
                    this.state.answers.length)
                ]
              });
            }} 
       />

my answers data structure is like= 0: {id: 799, answer_text: "helloas fast" percentage: 0}

i want to update the answer text and store it in the this.state.answers, but when i write text on textinput and update it i am getting only answer_text in hash and i want all id, answer_text, percentage in hash. So, please i need suggestion.

Thanks in advance.

1 Answer 1

2

You didn't handle your array data well. You can either keep its structure, or use answers_data.map to convert them into string.

Full code of option one:

constructor(props) {
  super(props);
  this.state = {
    answers: props.element.answers_data.map((answer, index) => {
      return { ...answer };
    })
  }
}

<TextInput
  value = {this.state.answers[index].answer_text}
  onChangeText={(answer_text) => {
          this.setState({
            answers: [
                ...this.state.answers.slice(0, index),
                { ...this.state.answers[index], answer_text },
                ...this.state.answers.slice(index + 1, this.state.answers.length)
            ]
          });
        }} 
 />

Full Code of option two:

constructor(props) {
  super(props);
  this.state = {
    answers: props.element.answers_data.map((answer, index) => {
      return answer.answer_text;
    })
  }
}

<TextInput
  value = {this.state.answers[index]}
  onChangeText={(answer_text) => {
          this.setState({
            answers: [
                ...this.state.answers.slice(0, index),
                answer_text,
                ...this.state.answers.slice(index + 1, this.state.answers.length)
            ]
          });
        }} 
 />
Sign up to request clarification or add additional context in comments.

3 Comments

my answers structure is like this = 0: {id: 777, answer_text: "hello", percentage: 0} 2:{id: 776, answer_text: "gb", percentage: 0} and when i am writing answer_text then it only gives me answer_text but i want this {id: 776, answer_text: "gb", percentage: 0} whole structure. please suggest me what i can write there to get this 3 value in one object
modified my answer for two possibility to change your code. check which one is more readable to you. feel free to ask here.
thank you so much for your response @val.. its working

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.