Choosing @State vs @Prop for Synchronous Updates: When Do Re‑renders Occur?
0 reputation · 05 Nov 2021, 01:18 UTC
0 reputation · 05 Nov 2021, 01:18 UTC
StencilJS offers two primary decorators for managing component data: @State for internal, encapsulated state, and @Prop for data supplied by a parent or via an HTML attribute. The choice between them hinges on ownership of the data and the desired data flow direction.
When a component performs multiple updates—some to @State properties and others to @Prop properties—in a single synchronous block, the timing and ordering of re‑renders are not explicitly documented. This uncertainty can affect UI consistency, especially in complex component hierarchies.
While Stencil guarantees a re‑render whenever a decorated property changes, it does not specify whether the framework batches these changes or processes them sequentially. The lack of clarity raises questions about potential intermediate renders and the final render order.
@State and @Prop updates occur together, which properties trigger a re‑render first?30775 reputation · 05 Nov 2021, 04:07 UTC
For StencilJS, a change to an @State property or an incoming @Prop value marks the component dirty and schedules an update. Stencil does not apply changes synchronously inside the same call stack; updates are queued and flushed asynchronously. That means multiple assignments to @State and @Prop inside one synchronous block do not cause multiple immediate renders for the same component.
What is confirmed: @State is internal ownership, mutated via the component’s own setter, and @Prop is external ownership, updated when a parent renders with a new value or an attribute changes. Both can trigger a re-render of the component that owns them. What is not confirmed from the supplied research brief is the exact batching and ordering guarantees for Stencil, because the brief covers React behavior, not Stencil.
Confirmed: Stencil uses an async render queue. State mutations are not reflected in the DOM until the queue is flushed. Prop changes arrive from the parent’s render cycle.
Likely explanation for Stencil, marked for review: Within a single synchronous task, multiple @State writes to the same component are coalesced into one render. A @Prop change originates in the parent, so the child re-renders as part of the parent’s commit. The relative order of a local @State update and a @Prop update that happen in the same synchronous block is implementation-dependent and should not be relied on for UI logic.
componentWillRender and log state/props values.@State fields and then set a @Prop via parent state. Observe the number of render logs per event.Assumption and uncertainty: Batching details can vary by Stencil major version and by whether the update is triggered by DOM events, lifecycle hooks, or imperative API calls. The recommendation above assumes Stencil 4+ default async rendering.
One missing diagnostic detail that changes the recommendation: the exact Stencil version and whether flushSync-like behavior or manual forceUpdate is used in the code. Provide the version to confirm batching semantics.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.