Repository navigation
Teach change detection with OnPush - #40
Merged
Merged
Conversation
Splitting files on '@component(' also matched the code samples the lessons print, so five strings counted as components. The parser only sees real class decorators, and a second test pins that.
All 35 components were checked on every pass. None of them holds state that changes outside signals or template events, so the switch needs no other code changes. The contact form's reset gets a comment: it renders only because a signal write sits next to it.
One plain object goes to an Eager card and an OnPush card. Mutating it updates only the Eager one; replacing it updates both; a shared signal reaches the OnPush card with no input change at all. The cards are styled from the lesson component. Sharing one styles constant between them left an esbuild context open in @angular/build 21.2.18, and ng build and ng test never exited.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ninth lesson in my Angular guide: change detection. The feature slice puts every component on
ChangeDetectionStrategy.OnPushand adds a test that keeps it there.What shipped
Every component is OnPush. None of the 35 components set a strategy before this, so all of them were checked on every pass.
Before switching, I audited each one for state that changes outside signals and template events, the kind OnPush stops rendering. I found none, so the switch is a one-line change per decorator.
form.reset()inside asetTimeout. It renders only becausesubmissionStatus.set('success')sits next to it. It now has a comment saying so.A fitness test keeps it that way.
architecture.spec.tsreads every@Componentdecorator with the TypeScript parser. It fails, naming the class, on any component that doesn't chooseOnPushor an explicitEager.@Component(, which also matched five code samples printed inside the lessons.Lesson 09: change detection (
/thoughts/angular-09-change-detection).markForCheckvsdetectChangesvsdetachDefaulttoEagerEagercard and anOnPushcard:Version and changelog. 1.4.8, with a matching changelog entry.
Correction, made in #42
The contact form comment in this PR is wrong.
form.reset()doesn't need the signal write next to it to re-render: reactive form controls keep touched and status in signals, andNgControlStatusreads them in its host bindings. A spec in #42 resets the form with no other change and nodetectChanges(), and the errors clear. The comment and the lesson 09 intro are corrected there, on top of this branch.A build hang I hit on the way
At first the two demo cards shared one
stylesconstant. With two components carrying byte-identical inline styles,@angular/build21.2.18 left its esbuild service process referenced.ng buildandng testboth finished their work (77 routes prerendered, 426 tests passed) and then never exited.I bisected it in a separate worktree:
A handle dump on the hung process showed the culprit:
The fix is to stop sharing the styles: the lesson component styles both card hosts, and the cards have no
stylesof their own. A comment at the template constant records why.Commits
Verification
ng test: 60 files, 426 tests, exit 0. That covers the demo's three behaviours and axe on the lesson page. No existing spec needed changes for OnPush.tsc --noEmitconfigs are clean.change-detectiontag page.TO-DOs for this PR