JavaScript Mutating nested object vuex mutation nested object

I'm trying not to mutate an object within an object before setting it to state like this:

isDraggable = () => {
        let steps = [...this.state.stepsData];

        steps = steps.map(step => {
            return {
                ...step,
                dataGrid.static: !step.dataGrid.static 
            }
        });

        this.setState({stepsData: steps})
 };

The object structure looks like this:

{
 stepsData:[{
  dataGrid: {
   x: ...
   y: ...
   static: true
  }
 }]
}

This line dataGrid.static: !step.dataGrid.static doesn't compile. How do I solve this? Thanks in advance!

Answer:1

You'll need to clone the object dataGrid refers to. Also note that you must use the function callback version of setState when you're setting state based on state:

isDraggable = () => {
    this.setState(prevState => {
        return {stepsData: prevState.steps.map(step => {
            return {
                ...step,
                dataGrid: {...step.dataGrid, static: !step.dataGrid.static}
            };
        })};
    });
};

Or more condensed but perhaps less clear:

isDraggable = () => {
    this.setState(prevState => ({
        stepsData: prevState.steps.map(step => ({
            ...step,
            dataGrid: {...step.dataGrid, static: !step.dataGrid.static}
        }))
    }));
};
Answer:2

You can overide the dataGrid key and spread the step.dataGrid

isDraggable = () => {
    this.setState(prevState => {

        const steps = prevState.stepsData.map(step => {
            return {
                ...step,
                dataGrid: {
                    ...step.dataGrid,
                    static: !step.dataGrid.static
                }
            }
        });

        return { stepsData: steps };
    })
};
Answer:3

I have a function that finds the longest word in a string. function findLongestWord(str) { var longest = str.split(' ').reduce((longestWord, currentWord) =>{ return currentWord.length > ...

I have a function that finds the longest word in a string. function findLongestWord(str) { var longest = str.split(' ').reduce((longestWord, currentWord) =>{ return currentWord.length > ...

  1. javascript reduce find max
  2. javascript reduce find min
  3. javascript reduce find
  4. javascript reduce to find average
  5. javascript array reduce find max
  6. javascript map filter reduce find
  7. javascript map find reduce
  8. js reduce find max

I've been trying to access any script in my ASP.NET (Web Forms) application but it doesn't work. I'm able to access the file through the server therefore the destination is fine but the file doesnt ...

I've been trying to access any script in my ASP.NET (Web Forms) application but it doesn't work. I'm able to access the file through the server therefore the destination is fine but the file doesnt ...

I'm getting small issue, once we click on button one modal popup displaying, but at the time of unload the overlay doesn't disappear. I tried with below code, could anyone suggest me, whats the issue ...

I'm getting small issue, once we click on button one modal popup displaying, but at the time of unload the overlay doesn't disappear. I tried with below code, could anyone suggest me, whats the issue ...

  1. overlay disappear on hover
  2. beamer overlay disappear

I can't figure out how to convert async await functionality in a while loop to a promise based implementation. repl showing the async await version https://repl.it/repls/IdealisticPepperyCoding ...

I can't figure out how to convert async await functionality in a while loop to a promise based implementation. repl showing the async await version https://repl.it/repls/IdealisticPepperyCoding ...

  1. convert async await to promise
  2. convert async await to callback
  3. convert async await to observable
  4. convert to async await
  5. babel convert async await
  6. convert then to async await
  7. convert promise to async await online
  8. convert callback to async await javascript
  9. convert promise into async await
  10. convert callback to async await nodejs
  11. convert callback to async await c#
  12. convert callback function to async await
  13. convert promise.all to async await
  14. convert promise chain to async await
  15. convert then catch to async await
  16. convert new promise to async await