Properties
Props are custom attributes/properties exposed publicly on an HTML element. They allow developers to pass data to a component to render or otherwise use.
The Prop Decorator (@Prop())
Props are declared on a component using Stencil's @Prop() decorator, like so:
// First, we import Prop from '@stencil/core'
import { Component, Prop, h } from '@stencil/core';
@Component({
tag: 'todo-list',
})
export class TodoList {
// Second, we decorate a class member with @Prop()
@Prop() name: string;
render() {
// Within the component's class, its props are
// accessed via `this`. This allows us to render
// the value passed to `todo-list`
return <div>To-Do List Name: {this.name}</div>
}
}
In the example above, @Prop() is placed before (decorates) the name class member, which is a string. By adding
@Prop() to name, Stencil will expose name as an attribute on the element, which can be set wherever the component
is used:
{/* Here we use the component in a TSX file */}
<todo-list name={"Tuesday's To-Do List"}></todo-list>
<!-- Here we use the component in an HTML file -->
<todo-list name="Tuesday's To-Do List"></todo-list>
In the example above the todo-list component is used almost identically in TSX and HTML. The only difference between
the two is that in TSX, the value assigned to a prop (in this case, name) is wrapped in curly braces. In some cases
however, the way props are passed to a component differs slightly between HTML and TSX.
Variable Casing
In the JavaScript ecosystem, it's common to use 'camelCase' when naming variables. The example component below has a
class member, thingToDo that is camelCased.
import { Component, Prop, h } from '@stencil/core';
@Component({
tag: 'todo-list-item',
})
export class ToDoListItem {
// thingToDo is 'camelCased'
@Prop() thingToDo: string;
render() {
return <div>{this.thingToDo}</div>;
}
}
Since thingToDo is a prop, we can provide a value for it when we use our todo-list-item component. Providing a
value to a camelCased prop like thingToDo is nearly identical in TSX and HTML.
When we use our component in a TSX file, an attribute uses camelCase:
<todo-list-item thingToDo={"Learn about Stencil Props"}></todo-list-item>
In HTML, the attribute must use 'dash-case' like so:
<todo-list-item thing-to-do="Learn about Stencil Props"></todo-list-item>
Data Flow
Props should be used to pass data down from a parent component to its child component(s).
The example below shows how a todo-list component uses three todo-list-item child components to render a ToDo list.
import { Component, Prop, h } from '@stencil/core';
@Component({
tag: 'todo-list',
})
export class TodoList {
render() {
return (
<div>
<h1>To-Do List Name: Stencil To Do List</h1>
<ul>
{/* Below are three Stencil components that are children of `todo-list`, each representing an item on our list */}
<todo-list-item thingToDo={"Learn about Stencil Props"}></todo-list-item>
<todo-list-item thingToDo={"Write some Stencil Code with Props"}></todo-list-item>
<todo-list-item thingToDo={"Dance Party"}></todo-list-item>
</ul>
</div>
)
}
}
import { Component, Prop, h } from '@stencil/core';
@Component({
tag: 'todo-list-item',
})
export class ToDoListItem {
@Prop() thingToDo: string;
render() {
return <li>{this.thingToDo}</li>;
}
}
Children components should not know about or reference their parent components. This allows Stencil to efficiently re-render your components. Passing a reference to a component as a prop may cause unintended side effects.
Mutability
A Prop is by default immutable from inside the component logic. Once a value is set by a user, the component cannot update it internally. For more advanced control over the mutability of a prop, please see the mutable option section of this document.
Types
Props can be a boolean, number, string, or even an Object or Array. The example below expands the
todo-list-item to add a few more props with different types.
import { Component, Prop, h } from '@stencil/core';
// `MyHttpService` is an `Object` in this example
import { MyHttpService } from '../some/local/directory/MyHttpService';
@Component({
tag: 'todo-list-item',
})
export class ToDoListItem {
@Prop() isComplete: boolean;
@Prop() timesCompletedInPast: number;
@Prop() thingToDo: string;
@Prop() myHttpService: MyHttpService;
}
Boolean Props
A property on a Stencil component that has a type of boolean may be declared as:
import { Component, Prop, h } from '@stencil/core';
@Component({
tag: 'todo-list-item',
})
export class ToDoListItem {
@Prop() isComplete: boolean;
}
To use this version of todo-list-item in HTML, we pass the string "true"/"false" to the component:
<!-- Set isComplete to 'true' -->
<todo-list-item is-complete="true"></todo-list-item>
<!-- Set isComplete to 'false' -->
<todo-list-item is-complete="false"></todo-list-item>
To use this version of todo-list-item in TSX, true/false is used, surrounded by curly braces:
// Set isComplete to 'true'
<todo-list-item isComplete={true}></todo-list-item>
// Set isComplete to 'false'
<todo-list-item isComplete={false}></todo-list-item>