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.

deleting object properties doesn't trigger a render

See original GitHub issue

Maybe this has been discussed at some point, maybe not, but I was surprised that

obj.b = 'foo';

…will trigger a re-render, but…

delete obj.b;

…won’t.

REPL: https://svelte.dev/repl/c5377d8c85e34735a792a29e1b2d1353?version=3.6.6

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

8reactions
pngwncommented, Jul 10, 2019

I think the simplest approach is = sign will trigger an update.

6reactions
argghcommented, Jul 10, 2019

After completing the tutorial a few weeks ago, the rules in the back of my head were:

  1. Fiddling with arrays or objects using methods won’t cause re-renders
  2. Fiddling with arrays or objects not using methods will cause re-renders

To me it’s more logical to group the different behaviors based on the fact that 1) are method calls, 2) are not.

// Won't re-render
arr.push(1);                              // as expected
arr.unshift(3);                           // as expected

// Will re-render
arr[3] = 4;                               // as expected

// Won't re-render
Object.assign(obj, { bar: 'foo' });       // as expected

// Will re-render
obj.foo = 'bar';                          // as expected
delete obj.foo;                           // Surprise!

I’m not going to die on this hill, but just wanted to point this out as being the more logical approach in my opinion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setState does not trigger render after item deleted from object
In my app you can set an age restriction on a link. If there is an age restriction on the link will it...
Read more >
Removing Object Properties with Destructuring
Before destructuring, we would typically use the delete keyword to remove properties from an object. The issue with delete is that it's a...
Read more >
When does React re-render components? - Felix Gerschau
Directly mutating the props object is not allowed since this won't trigger any changes, and React doesn't notice the changes.
Read more >
5 things you need to know about the delete operator in ...
The delete operator removes a property from an object. It cannot delete a variable. Any property declared with var cannot be deleted from ......
Read more >
React.useEffect Hook – Common Problems and How to Fix ...
Most developers have gotten pretty comfortable with how they work and ... We are actually creating a new object every time we re-render...
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