Invert boolean variable React.js

Asked

Viewed 54 times

0

onUpdateItem = i => {
    this.setState(state => {
        const subtarefa = state.subtarefa.map((item, j) => {
            if (j === i) {
                item.status = !item.status
                return item;
            } else {
                return item;
            }
        });

        return {
            subtarefa
        };
    });
    
};

'Cause that part doesn’t work?

item.status = !item.status
  • Could you present what problem is happening? Apparently, only with what was provided the problem consists of the missing character ; at the end of the instruction.

  • is not reversing the value of the variable

  • 1

    @Natãlopes You are sure that the execution is falling on this line of code?

  • i put a console.log and do not know pq but the function is running twice in that part so in case it changes and dps return the value

  • It’s so hard to know just by looking at the code, but there’s the condition that might not be satisfying and the other thing too the variable might not really be being altered by the immutable factor (maybe). Put the whole component in the question !!!

1 answer

0


Hello, does not work pq in React you use the concept of STATES to update or not the elements of your page.

Probably, you don’t see the updated item because no state has changed, so it is not rendered again. The solution is to create a state to control it or, put its variables in a state, remembering that it can be an entire object, array or other...

To upgrade, you can use frameworks or manually do the whole setState.

Recommend: https://stackoverflow.com/questions/29537299/react-how-to-update-state-item1-in-state-using-setstate

Basically copies its state, makes modifications and updates its state.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.