Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vanilla JS challenge

Vanilla JS challenge

tomoaki kanayama

February 11, 2024
Tweet

Other Decks in Programming

Transcript

  1. だれ 金山智昭 (Kanayama Tomoaki) 三重県津市在住のプログラマーです Frontend(React, Vue, webgl) Apps (Electron,

    ReactNative) Cloud (AWS, Firebase, Supabase) Backend (django, RoR) GeoVisualization Agile
  2. コンポーネント指向の開発 React // index.jsx function HelloWorld(props) { return <span className="title">Hello!

    {props.name}<span> } function Root() { return <div> <HelloWorld name="world"> <HelloWorld name="vanilla js"> </div> }
  3. コンポーネント指向の開発 Web component <!-- DOMを独自に定義 --> <hello-world name="world"> <hello-world name="Vanilla

    JS"> class HelloWorld extends HTMLElement { connectedCallback() { const shadow = this.attachShadow({ mode: 'closed' }); // このコンポーネント専用のCSSが書ける shadow.innerHTML = /* html */ ` <style> span { text-align: center; } </style> <span>Hello ${ this.name }!</span>`; } } defineComponent('hello-world', HelloWorld)
  4. Reactive Web component <Counter id="counter" count="0"></counter> class Counter extends HTMLElement

    { // 属性の値が変わったら更新される attributechangedcallback() { const shadow = this.shadowRoot; const count = this.getAttribute('count') shadow.innerHTML = /* html */ ` <span>Count: ${ count }!</span>`; } } const counter = document.getElementById('counter') counter.setAttribute("count", "6")