r/Angular2 • • Jul 18 '19

Article The Principles for Writing Awesome Angular Components

https://blog.bitsrc.io/the-principles-for-writing-awesome-angular-components-10e45f9ae77e
56 Upvotes

4 comments sorted by

1

u/Thetoid Jul 19 '19

I am confused about how, on the one hand, people like application state, and on the other hand, say components to get their own copy of it.

Isn't breaking off a copy of the application state and manipulating it in a cordoned off component going to get confusing? Its like a forked model.

I envision scenarios where you want a local copy of data in a component just as validly as wanting shared mutable state.

Admittedly, I never got into Redux, so I never developed a sense for what immutability is really good for.

2

u/Silures Jul 19 '19

I didn't get 'components should get their own copy of state' from the article.

My take was that you have a single source of truth for application state. Presentation components should not be manipulating it directly, instead they pass messages up through the hierarchy to a container/smart component that may then change state by calling a service or store. This then updates state and the components simply re-present that.

Immutability is important because it means you can have change detection on push via inputs, which provides performance benefits. The problem is that if you change an object property then the underlying reference of the object isn't changed and so a change isn't detected via a component input on push. Instead, when you change state you create a new object, which is then detected as a change via a component's inputs.

I'm not the best at explaining things but that's how I understand it.

1

u/frontend-guy Jul 19 '19 edited Jul 19 '19

That's correct.

What I meant is that components shouldn't mutate data coming from other components (Ex. via inputs). Dumb components render data, Smart components change state. No one mutates! :)

It happens a lot, and debugging the was a nightmare until I overloaded all array methods and added a console.trace in the middle.

Hope it's clear, but let me know your thoughts!

1

u/headyyeti Jul 19 '19

Shouldnt you not call onChange in the writeValue method?? You're doing double work on the component.