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.

[Tutorial] Add extra explanation for object reactivity

See original GitHub issue

Is your feature request related to a problem? Please describe. There are users that are confused about how reactivity work with objects, to be more precise:

const childRef = obj.foo;
// The tutorial don't explicity says that if now 
// I modify childRef it will not create an invalidation of obj
childRef.bar = "new value"; // Users expect this to work, because
// obj.foo.bar = "new value"; // this updates as they expect, but childRef don't
// So the point its to let know new users that:
obj = obj;
// Will be needed in this case to update it, or work directly with the top level obj

Describe the solution you’d like Adding to the tutorial 02-reactivity/04-updating-arrays-and-objects a better explanation about this case would better inform new users.

The tutorial speaks about how array methods don’t create an invalidation, but this case can’t be understanded just with the explanation given.

The user @halfnelson said a rule for this case that would help new users:

[…] it can be tricky until you are used to it but the rule is broadly “the name of the reactive variable must appear on the left of the equals to react to assignments”

Describe alternatives you’ve considered An option would be to make derivated references also be reactive, but that calls for problems and I feel that just explaining to new users how it works its easier than changing the underlaying behaviour.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Albermontecommented, Dec 9, 2021

My bad then, it’s a little counter-intuitive seeing the code thinking it’s ok. Maybe a comment on the code saying that it’s not working would be helpful imo

1reaction
Conduitrycommented, Dec 21, 2019

Like anything where we track the identity of the objects ending up in different variables, this will open a huge can of worms. We’d never be able to catch all such situations, and it’s better to have a reasonable, well-defined goal than an impossible one that we gradually approach. We’d either have to declare that Svelte will always an unfixable bug, or we’d have to incrementally make small, subtle breaking changes.

In this case, if someone later did obj.foo = something_else; (or, even harder to detect, did obj[baz] = something_else; when baz === 'foo') would this result in updates to childRef no longer invalidating obj? What if the mutation of obj happened in code that wasn’t even part of this component? This is the sort of thing that we don’t even want to get into - and so we establish precise and very achievable rules for when reactivity happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactivity in Depth - Vue.js
Vue's reactivity system works by deeply converting plain JavaScript objects into reactive proxies. The deep conversion can be unnecessary or sometimes unwanted ...
Read more >
Understanding the New Reactivity System in Vue 3 - SitePoint
Learn why Vue 3's new, feature-rich reactivity API is far more flexible and powerful ... Add a new property to an object addAgeProperty() ......
Read more >
Your guide to reactivity in Vue.js - LogRocket Blog
Get a better understanding of reactivity in Vue.js and see how to achieve ... Here, we added two objects: data and newData (empty)....
Read more >
Reactivity / Updating arrays and objects • Svelte Tutorial
Svelte's reactivity is triggered by assignments. Methods that mutate arrays or objects will not trigger updates by themselves. In this example, clicking the ......
Read more >
Reactivity In Vue - Smashing Magazine
Reactivity is the ability for a variable (array, string, number, object, etc) to update when its value or any other variable that it...
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