Overview
Essentials
- Installation
- Introduction
- Defining a Component
- Property Decorators
- Manipulating DOM
Reusability
- Composition
- Custom Decorators
Architecture
- Event Handling
- State Management
Internals
- Component Lifecycle
- Initialisation Process
Tooling
- Debugging
- Deployment
- CLI
Miscellaneous
- Migration from Strudel 0.x
- Style Guide
Composition
Sharing functionality between components
Strudel expects a single component to be attached to a single DOM node. Which is fine if you think about components, as single responsibility entities. However sometimes there are cases where you would like to have more functionality on a single DOM node, how can you organise your code in such scenario?
In this guide we will cover Strudel mixins - way of sharing functionality between components.
Mixins
To inject a mixin into a component you simply need to define mixins
property inside the class and as a value provide an array of mixins that you would like to use. Mixins are just plain JavaScript objects (class mixins are not supported). A mixin object can contain any component properties, that will be later injected into the main component.
const dictionary = { |
Mixins with lifecycle hooks
When a lifecycle hook is defined inside a mixin - like init
instead of mixing it as a regular property, such hook will be executed during component initialisation. It allows mixins to introduce logic to components and run it without any additional overhead.
const makeRed = { |
Common usecases
Mixins should come useful when sharing some accessibility functionality, reusable interactions or behaviour between components. There is no limit in how many mixins you would like to have and use in a single component.