LazyDal
LazyDal

Reputation: 189

Problems with array as state in React

jsx transforming tool throws an error saying 'unexpected token: [' on the following line:

this.setState({array[index]: value});

Obviously I want simply to set new state but this error forced me to write some workarounds. Why isn't the transformer accepting the above line?

Upvotes: 1

Views: 88

Answers (1)

Anders Ekdahl
Anders Ekdahl

Reputation: 22933

Simply because it's not valid Javascript (or JSX for that matter). You want something like this:

array[index] = value;
this.setState({array: array});

Upvotes: 5

Related Questions