0

I have following Table / Array: enter image description here

If I press the blue button, then all items with the same group as the record should change the Status (Gratis). But now it just change the Value of the Record and all items above it. As an example, if I press the Button on Record No. 1 then itselft and all above (No. 0) get an change of the Status (Gratis).

Following code im using to go through the array and change the Status:

private _updateFreeStatus = (record: QuestionModel): void => {
        fetch('api/Test/UpdateGratisStatus', {
            headers: { 'Content-Type': 'application/json' },
            method: 'PUT',
            body: JSON.stringify({
                'group': record.group,
                'free': record.free,
            })
        });
        this.state.question.map(item => {
            if (item.group === record.group)
            {
                item.free = !record.free;
            }
        });
    }
3
  • What is this.state.question and record? Commented Dec 16, 2021 at 21:30
  • @larz this.state.question is the Array of Type QuestionModel, and record is the selected record in the table (also Model of QuestionModel) Commented Dec 16, 2021 at 21:32
  • as it is written fetch makes the request but you don't do anything with it. to read the response, you have to attach a .then handler or use async..await. Commented Dec 16, 2021 at 21:35

1 Answer 1

1
  1. do not mutate the state
  2. create a copy, and use setState

Use

const updatedQuestions = this.state.question.map(item => {
  if (item.group === record.group) {
    return {
       ...item,
       free: !record.free
    }
  }
  return item;
});

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

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.