cheatsheets/stencil.md

206 lines
3.8 KiB
Markdown
Raw Permalink Normal View History

2017-10-11 03:09:36 +00:00
---
title: Stencil
category: JavaScript libraries
2020-07-04 13:33:09 +00:00
updated: 2019-05-08
2017-10-16 15:08:27 +00:00
keywords:
2017-10-17 03:44:52 +00:00
- "@Component"
- "@Prop()"
- "@State()"
- "render()"
- "componentWillLoad()"
- "componentWillUpdate()"
- "Templating"
- "Lifecycle"
2017-10-11 03:09:36 +00:00
intro: |
[Stencil](https://github.com/ionic-team/stencil) is a compiler for web components made by the Ionic team. This guide targets Stencil v0.0.5.
---
## Quick-start guide
{: .-three-column}
### Getting started
{: .-prime}
#### JavaScript
```js
import { Component, Prop, State } from '@stencil/core'
@Component({
tag: 'my-component',
styleUrl: 'my-component.scss'
})
export class MyComponent {
@Prop() name: string
@State() isVisible: boolean = true
render () {
return <p>I am {this.name}!</p>
)
}
}
```
#### HTML
```html
<my-component name='Groot' />
```
That's the same example in the [Readme](https://github.com/ionic-team/stencil), that's as simple as you can get! Just use `<my-component>` like you would use any other HTML tag.
2017-10-11 03:09:36 +00:00
### DOM events
```js
export class MyComponent {
render () {
return (
<input
onChange={(event: UIEvent) => this.inputChanged(event)}
/>
)
}
inputChanged (event) {
console.log('input changed:', event.target.value)
}
}
```
{: data-line="5,10,11"}
Stencil uses DOM events.
See: [Handling user input](https://stenciljs.com/docs/templating/#handling-user-input)
### Multiple children
```js
render () {
return [
<h1>Hello there</h1>,
<p>This component returns multiple nodes</p>
]
}
```
{: data-line="3,4"}
`render()` can return an array of elements.
See: [Complex template content](https://stenciljs.com/docs/templating#complex-template-content)
## State
### Managing state
```js
export class MyComponent {
@State() isVisible: boolean
show () {
this.isVisible = true
}
}
```
{: data-line="4,5"}
2017-10-11 04:00:03 +00:00
Just do assignments. You can't do mutations though, see next section.
2017-10-11 03:09:36 +00:00
See: [Managing component state](https://stenciljs.com/docs/decorators#managing-component-state)
### Updating arrays and objects
2017-10-11 04:02:54 +00:00
#### ✗ Bad
2017-10-11 03:09:36 +00:00
```js
2017-10-11 04:02:54 +00:00
this.names.push('Larry') // ⚠️
this.options.show = true // ⚠️
2017-10-11 03:09:36 +00:00
```
2017-10-11 04:02:54 +00:00
#### ✓ OK
2017-10-11 03:09:36 +00:00
```js
2017-10-11 04:02:54 +00:00
this.names = [ ...this.names, 'Larry' ]
this.options = { ...this.options, show: true }
2017-10-11 03:09:36 +00:00
```
Mutable operations such as `push()` won't work. You'll need to assign a new copy.
2019-05-08 04:37:31 +00:00
See: [Updating arrays](https://stenciljs.com/docs/reactive-data/#updating-arrays)
2017-10-11 03:09:36 +00:00
## Slots
### Using slot
```html
<my-component>
<span>Hello, friends</span>
</my-component>
```
{: data-line="2"}
#### Component
```js
render() {
return <h1><slot /></h1>
}
```
{: data-line="2"}
You can pass JSX/HTML as child elements. Use the `slot` tag to use them inside your component.
See: [Slots](https://stenciljs.com/docs/templating#slots)
### Multiple slots
```html
<my-component>
<p slot='my-header'>Hello</p>
<p slot='my-footer'>Thanks</p>
</my-component>
```
{: data-line="2,3"}
#### Component
```js
render () {
return <div>
<header><slot name='my-header' /></header>
<footer><slot name='my-footer' /></footer>
</div>
}
```
{: data-line="3,4"}
See: [Slots](https://stenciljs.com/docs/templating#slots)
## Lifecycle
2017-10-11 03:25:09 +00:00
### Lifecycle hooks
2017-10-11 03:09:36 +00:00
| Event | Description |
| --- | --- |
2017-10-16 15:08:27 +00:00
| `componentWillLoad()` | Before rendering |
2017-10-11 03:09:36 +00:00
| `componentDidLoad()` | After rendering |
| --- | --- |
| `componentWillUpdate()` | Before updating |
| `componentDidUpdate()` | After updating |
| --- | --- |
| `componentDidUnload()` | After unmounting |
See: [Component lifecycle](https://stenciljs.com/docs/component-lifecycle)
2017-10-11 03:25:09 +00:00
### Example
```js
export class MyComponent {
componentWillUpdate () {
console.log('updating')
}
}
```
2017-10-11 03:09:36 +00:00
## References
- [Stencil docs](https://stenciljs.com/docs/) _(stenciljs.com)_