0

I have the following method:

  addWidget = (index) => {
    var currentState = this.state;

    if(currentState.availableWidgets[index].pos === 'start'){

      // add it at the start
      for(var i = 0; i < currentState.widgets.length; i++){
        this.setState({
          widgets: [
            ...currentState.widgets,
            currentState.widgets.x = 5
          ]

        })
      }
    }
    else {
      var endX = currentState.widgets.reduce((endX, w) => endX + w.w, 0)
      if (endX === 12) endX = 0

      this.setState({
        widgets: currentState.widgets.concat({
          ...currentState.availableWidgets[index],
          i: uuid(),
          x: endX,
          y: Infinity,
        })
      })
    }

    console.log(currentState.widgets);
  }

and the state is:

class DashboardContainer extends React.Component {
  state = {
    widgets: [],
    availableWidgets: [
      {
        type: 'compliance-stats',
        config: {

        },
        w: 1,
        h: 1,
        pos: 'start',
      },
      {
        type: 'compliance-stats',
        config: {

        },
        w: 3,
        h: 2,
      }
    ]
  }
  ...

I am trying to update the "x" property of each object inside "widgets" by doing so:

  for(var i = 0; i < currentState.widgets.length; i++){
    this.setState({
      widgets: [
        ...currentState.widgets,
        currentState.widgets.x = 5
      ]

    })
  }

I am aware of setting the state inside a loop is not good at all. However I am currently getting an error.

enter image description here

I am importing widgets in:

const Dashboard = ({ widgets, onLayoutChange, renderWidget }) => {
  const layouts = {
    lg: widgets,
  }

  return (
    <div>
      <ResponsiveReactGridLayout 
        layouts={layouts} 
        onLayoutChange={onLayoutChange} 
        cols={{ lg: 12 }} 
        breakpoints={{lg: 1200}}>
          {
            widgets.map(
              (widget) => 
              <div key={widget.i}>
                {renderWidget(widget)}
              </div>
            )
          }
      </ResponsiveReactGridLayout>
    </div>
  )
}
3

2 Answers 2

3

Probably better to change the widgets and then setState only once:

const changedWidgets = currentState.widgets.map(w => ({ ...w, x: 5 }));
this.setState({ widgets: changedWidgets });
Sign up to request clarification or add additional context in comments.

3 Comments

This is a great solution but it's complaining about the spread operator
I had to add a return "w => { return {...w, x: w.x + currentState.availableWidgets[index].w } }"
Fixed the return of the object literal
0

The spread operator inside an array will concatenate a new value onto the array, so by seting widgets state to [ ...currentState.widgets, currentState.widgets.x = 5 ] what you're actually trying to do is currentState.widgets.concate(currentState.widgets.x = 5) which is throwing your error.

If you would like to modify the value inside an array you should map out the array and then modify the objects inside the array like so.

const widgets = currentState.widgets.map(widget => { ...widget, x: 5})
this.setState({ widgets })

1 Comment

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.