question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding items to array does not trigger _render call

See original GitHub issue

Description

When changing array data the change is not recognized until _requestRender() is called

Live Demo

https://glitch.com/edit/#!/bold-cuticle

Steps to Reproduce

  1. create my-element
  2. create an array property
  3. use the array property in the _render function
  4. 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:closed
  • Created 5 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
nicolasparadacommented, Jun 18, 2018

This is the expected behavior. On Polymer they have this.push. On lit-element you can do:

this.colors = [...this.colors, 'green']

To override the complete array instead of mutating it.

2reactions
sorvellcommented, Aug 27, 2018

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:

static get properties() {
  foo: {shouldInvalidate: (value, oldValue) => value.key !== oldValue.key;}
}

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.

static get properties() {
  foo: { shouldInvalidate: (value, oldValue) => true }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found