chore: add template vs render() section to readme

This commit is contained in:
Ayo 2023-09-22 09:55:13 +02:00
parent 86f77d86ca
commit 16108193fd
3 changed files with 13 additions and 3 deletions

View file

@ -16,6 +16,7 @@ The result is a reactive UI on property changes.
1. [Import via unpkg](#import-via-unpkg) 1. [Import via unpkg](#import-via-unpkg)
1. [Installation via npm](#installation-via-npm) 1. [Installation via npm](#installation-via-npm)
1. [Usage](#usage) 1. [Usage](#usage)
1. [`template` vs `render()`](#template-vs-render)
1. [Quick Start Example](#quick-start-example) 1. [Quick Start Example](#quick-start-example)
1. [Life-Cycle Hooks](#life-cycle-hooks) 1. [Life-Cycle Hooks](#life-cycle-hooks)
1. [`onInit`](#oninit) - the component is connected to the DOM, before view is initialized 1. [`onInit`](#oninit) - the component is connected to the DOM, before view is initialized
@ -82,6 +83,15 @@ The result is a reactive UI that updates on attribute changes:
<img alt="UI showing feeling toward Web Components changing from SAD to EXCITED" src="https://git.sr.ht/~ayoayco/web-component-base/blob/main/assets/wc-base-demo.gif" width="400" /> <img alt="UI showing feeling toward Web Components changing from SAD to EXCITED" src="https://git.sr.ht/~ayoayco/web-component-base/blob/main/assets/wc-base-demo.gif" width="400" />
## `template` vs `render()`
This mental model attempts to reduce the cognitive complexity of authoring components:
1. The `template` is a read-only property (initialized with a `get` keyword) that represents *how* the component view is rendered. This makes the template close to modification.
1. There is a `render()` method that triggers a view render.
1. This `render()` method is *automatically* called under the hood every time an attribute value changed.
1. You can *optionally* call this `render()` method at any point to trigger a render if you need.
## Quick Start Example ## Quick Start Example
Here is an example of using a custom element in a single .html file: Here is an example of using a custom element in a single .html file:
@ -179,4 +189,4 @@ class ClickableText extends WebComponent {
return `<span style="cursor:pointer">Click me!</span>`; return `<span style="cursor:pointer">Click me!</span>`;
} }
} }
``` ```

View file

@ -1,6 +1,6 @@
// @ts-check // @ts-check
import WebComponent from "../dist"; import WebComponent from "../src/index.js";
export class HelloWorld extends WebComponent { export class HelloWorld extends WebComponent {
name = "World"; name = "World";

View file

@ -1,6 +1,6 @@
// @ts-check // @ts-check
import WebComponent from "../dist/index"; import WebComponent from "../src/index.js";
class SimpleText extends WebComponent { class SimpleText extends WebComponent {
onInit() { onInit() {