createApp pattern provides a centralized way to manage application state using reducers and an event-driven architecture. This approach is ideal for simple applications that need predictable state updates.
Basic Usage
Create an application with initial state, reducers, and a top-level view:Configuration Object
ThecreateApp function accepts a configuration object:
State
The initial state of your application. Can be any JavaScript object:View Function
The view function receives the current state and emit function:Reducers
Reducers are pure functions that take the current state and a payload, returning the new state:Reducers must be pure functions - they should not modify the existing state directly. Always return a new state object.
The emit Pattern
Useemit to dispatch actions that trigger reducers:
- The corresponding reducer is called with the current state and payload
- The reducer returns the new state
- The view is re-rendered with the updated state
Complete Todo Example
App Instance Methods
The app instance provides three methods:mount(element)
Mounts the application to a DOM element:unmount()
Unmounts the application and cleans up all subscriptions:emit(eventName, payload)
Emits an action from outside the view:When to Use createApp
ThecreateApp pattern is best for:
- Simple applications with a single top-level view
- Centralized state that follows a predictable update pattern
- Event-driven architectures where actions flow in one direction
- Learning GlyphUI as it provides a complete app structure
For larger applications with multiple components that need shared state, consider using global stores with the connect pattern instead.
Next Steps
- Explore global stores for component-based state management
- Learn about connecting components to shared state
- Review local state for component-specific data