Closed christophehenry closed 1 year ago
Very similar to this previous question. https://github.com/hotwired/stimulus/issues/593
This way of modifying values is not the documented approach. Instead of thinking of the fooValue as an instance value, remember they are a getter and setter pair.
When retrieving someValue, it is reading from the DOM, when you set it by using this.someValue = ['new'];
you are writing the DOM. There are no instance values being mutated.
You are best to avoid confusion by writing to the array as per the documentation.
const arrValue = this.arrValue; // reminder. Will error if no value set in the DOM, intentionally.
arrValue.push('test');
this.arrValue = arrValu;
This should work as expected. Here you are first getting the value, then mutating it, finally setting it back.
Maybe I missed something but this snippet is not in the documentation, is it?
Maybe it could be clearer in the docs but it does state that the fooValue methods are getters and setters.
https://stimulus.hotwired.dev/reference/values#properties-and-attributes
Ok, thank you. Closing, then.
Hi! I just stumbled upon a very strange behavior of values: objects and array values can't be modified unless a default value is provided.
Here is a simple example:
This will print:
Maybe this is expected but this is not what's documented.