Component Types
Jaspr provides three core component types, each serving a different purpose:StatelessComponent
AStatelessComponent is used when your UI doesn’t need to maintain any mutable state. It’s the simplest component type and is ideal for static content or components that only depend on their constructor parameters.
- All fields must be
final - No mutable state
- The
buildmethod is called when:- The component is first inserted into the tree
- The parent changes the component’s configuration
- An
InheritedComponentit depends on changes
- Static content that doesn’t change
- Components that only transform input parameters into UI
- Presentational components without internal state
StatefulComponent
AStatefulComponent is used when your UI needs to maintain mutable state that can change over time. The component itself remains immutable, but it creates a separate State object to hold mutable data.
- Component class is immutable and creates a
Stateobject - State object holds mutable data
- Use
setState()to update state and trigger a rebuild - Lifecycle methods available:
initState,didChangeDependencies,didUpdateComponent,dispose
createState()- Creates the State objectinitState()- Called once when the State is createddidChangeDependencies()- Called after initState and when dependencies changebuild()- Called to render the UI (can be called multiple times)didUpdateComponent()- Called when the parent rebuilds with a new componentdeactivate()- Called when removed from the treedispose()- Called when permanently removed
- Forms and user input
- Interactive UI elements (buttons, toggles, etc.)
- Components that need to respond to user events
- Components with animations or timers
InheritedComponent
AnInheritedComponent efficiently propagates data down the component tree. Descendant components can access this data and automatically rebuild when it changes.
- Must have a
childcomponent - Descendants can access the inherited data using
dependOnInheritedComponentOfExactType<T>() - Descendants automatically rebuild when
updateShouldNotify()returnstrue - Provides a way to share data without passing it through every component
- Sharing configuration data (themes, localization)
- Providing services to descendants (authentication, routing)
- Managing global or cross-cutting state
- Avoiding prop drilling
Component Keys
Keys control how components are matched when rebuilding. They’re useful for preserving state when components move in the tree.Key Types
- ValueKey - Based on a specific value (string, number, etc.)
- GlobalKey - Unique across the entire app, allows accessing state from anywhere
Performance Tips
Use const constructors
Use const constructors
Mark components and their constructors as
const whenever possible. This allows Jaspr to skip rebuilding identical components.Minimize widget tree depth
Minimize widget tree depth
Keep your component trees shallow. Extract reusable pieces into separate components rather than building deeply nested trees.
Cache components in State
Cache components in State
Store components that don’t change in final variables rather than recreating them on every build.
Implement shouldRebuild
Implement shouldRebuild
Override
shouldRebuild() to skip unnecessary rebuilds when component properties haven’t meaningfully changed.Related
HTML Elements
Learn how to render HTML with DomComponent
Styling
Style your components with the Styles class
Data Fetching
Load and manage asynchronous data
Routing
Navigate between pages in your app