Adding items to array does not trigger _render call
See original GitHub issueDescription
When changing array data the change is not recognized until _requestRender()
is called
Live Demo
https://glitch.com/edit/#!/bold-cuticle
Steps to Reproduce
- create my-element
- create an array property
- use the array property in the _render function
- add items to the array in an event listener function
Expected Results
The _render function is called
Actual Results
Nothing happens
Browsers Affected
- Chrome
- Firefox
- [ ?] Edge
- Safari 11
- [? ] Safari 10
- [ ?] IE 11
Versions
- lit-element: v0.5.2
- webcomponents: vX.X.X
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Adding items to array does not trigger _render call #107 - GitHub
Description When changing array data the change is not recognized until _requestRender() is called Live Demo https://glitch.com/edit/#!
Read more >React does not re-render updated array state - Stack Overflow
It's because the instance of the items array is not changing. Try this: setItems([...items]);. That copies the array to a new identical ...
Read more >Warning: Each Child in a List Should Have a Unique 'key' Prop
When creating a list in the UI from an array with JSX, you should add a key prop to each child and to...
Read more >When does React re-render components? - Felix Gerschau
Changing the state means that React triggers an update when we call the setState function (in React hooks, you would use useState )....
Read more >How to Add to an Array in React State using Hooks
“ useState is a Hook […] We call it inside a function component to add some local state to it. React will preserve...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is the expected behavior. On Polymer they have
this.push
. On lit-element you can do:To override the complete array instead of mutating it.
Out of the box, LitElement uses a strict equals check to see if a property has changed. This means it’s ideally suited to immutable data patterns as noted above.
However, sometimes, you need more control over this, so LitElement 0.6 will be exposing a property-based invalidation function so you can decide exactly when and if setting a property should trigger an invalidation and rendering. For example, if a property is always an object that should only trigger rendering if a
key
property changes, you could do this:To make any set to a mutating object/array typed property always trigger invalidation, you could do something like the following. Please note that for performance reasons you should use this pattern sparingly because it can sometimes can result in a large amount of unnecessary work.