ã¯ããã«
ãæ¬è¨äºã§ã¯ãã¦ã¼ã¶ã¼ã¤ã³ã¿ã¼ãã§ã¤ã¹ãæ§ç¯ããããã®JavaScriptãã¬ã¼ã ã¯ã¼ã¯ã®ã²ã¨ã¤ãSvelteï¼ã¹ãã«ãï¼ãã«ã¤ãã¦ãç´¹ä»ãã¾ãã
ãWebããã³ãã¨ã³ãã®é åã¯å¹´ã 大ãããªã£ã¦ãããèªè ã®çãã¾ã®ä¸ã§ãReactãVueã¨ãã£ããã¬ã¼ã ã¯ã¼ã¯ã使ã£ããã¨ãããæ¹ãå¤ããã®ã¨æãã¾ãããããããããSvelteã®ååãã©ããã§ã覧ã«ãªããæ°ã«ãªã£ã¦ããæ¹ãããããããã¾ããã
ãSvelteã¯ããã®ã¢ããã¼ãã®æ°ãããã注ç®ããã¯ããã¦ãã¾ãã JavaScript ã®ã©ã¤ãã©ãªã«é¢ãã大è¦æ¨¡èª¿æ»ãState of JS 2020ãã§ãæãæããã¦ããWebãã¬ã¼ã ã¯ã¼ã¯ãããã£ã¨ãéçºè ã®æºè¶³åº¦ã®é«ããã¬ã¼ã ã¯ã¼ã¯ãã«é¸ã°ãããã¨ã§ã話é¡ã¨ãªãã¾ããã
ãããã§æ¬è¨äºã§ã¯ãReactãVueã«å°ãã§ã触ãããã¨ãããæ¹ãæ³å®ãã¦ããããã¨æ¯è¼ããå½¢ã§ãSvelteã®ç¹å¾´ããããããã説æãã¦ãããã¨æãã¾ãã
ãã¾ããç¶ã第2åã»ç¬¬3åã§ã¯ãå®éã«Svelteã使ç¨ãã¦ã¢ããªã±ã¼ã·ã§ã³ãéçºããæé ã説æãã¾ããç¶ãã¦ãèªã¿ããã ããã¨ã§ãSvelteã«ããã¢ããªã±ã¼ã·ã§ã³éçºã®æµããä¸éãç解ã»ä½é¨ãã¦ããã ããæ§æã¨ãªãã¾ãã
Svelteã®ç¹å¾´
æ®éã®HTMLï¼JavaScript ã«è¿ãæ¸ãæ¹ãã§ãã
ãSvelteã®ç¹å¾´ã®ä¸ã¤ã¯ãã¦ã¼ã¶ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«å¿ é ã®ç¶æ 管çãä¼´ãã³ã¼ãããç¹å¥ãªè¨æ³ãé¢æ°ã使ããã¨ãªãå®è£ ã§ãããã¨ã§ããä¾ãã°ããã¿ã³ãæ¼ãã¦æ°å¤ãã«ã¦ã³ãã¢ããã»ã«ã¦ã³ããã¦ã³ããã³ã¼ãããSvelteãReactãVueã§è¦æ¯ã¹ã¦ã¿ã¾ãããã
// Svelte <script lang="ts"> let count = 0; </script> <div> <p>ã¯ãªãã¯ããåæ°: {count}</p> <button on:click={() => count += 1}>ã¯ãªãã¯ãã¦ãã ãã</button> </div> // React function App() { const [count, setCount] = useState(0); return ( <div> <p>ã¯ãªãã¯ããåæ°: {count}</p> <button onClick={() => setCount(count + 1)}> ã¯ãªãã¯ãã¦ãã ãã </button> </div> ); } // Vue <script setup> import { ref } from 'vue' const count = ref(0) </script> <template> <div> <p>ã¯ãªãã¯ããåæ°: {{ count }}</p> <button v-on:click="count++">ã¯ãªãã¯ãã¦ãã ãã</button> </div> </template>
ãããããããã¯ãªãã¯ãã¦ãã ãããã¨æ¸ããããã¿ã³ãæ¼ãã¨ããã¯ãªãã¯ããåæ°ãã®å³å´ã«è¡¨ç¤ºããã¦ããæ°å¤ãã«ã¦ã³ãã¢ãããã¦ããã¾ããå®éã«åä½ãã¦ããã¨ããã¯ãSvelteã¯å ¬å¼ã®REPLãããReactã§ã¯CodePenãã確èªãããã¨ãã§ãã¾ãã以ä¸ã§ã¯ä¸»ã«Reactã¨ã®å¯¾æ¯ã§è§£èª¬ãã¾ãããVueã®ä¾ãå ¬å¼ã®REPLãã確èªã§ãã¾ãã
ãReactã«æ¯ã¹ã¦Svelteã®ã³ã¼ãã®è¡æ°ãå°ãªããã¨ãåªããç¹ã§ããããã以ä¸ã«ãå¾æ¥ã®Webéçºã§æ £ã親ããã§ãããæ®éã®HTMLã¨æ®éã®JavaScriptãã®è¦ãç®ã«ããªãè¿ããã¨ã«ã注ç®ã§ãã
ãReactã§ã¯ãã³ã³ãã¼ãã³ãã¯ã¯ã©ã¹ãé¢æ°ã¨ãã¦å®ç¾©ãããJSXã¨ããHTMLã«ä¼¼ããã¼ã¯ã¢ããè¨èªãè¨è¿°ãããã¨ã§ãã³ã³ãã¼ãã³ãã®HTMLä¸ã§ã®ææ¸æ§é ï¼DOMæ§é ï¼ãå®ç¾©ãã¾ãããã®JSXã¯ãã¼ã¯ã¢ããè¨èªã®å®£è¨æ§ã¨åçè¨èªã®è¡¨ç¾åãèåãããåªãããã¯ããã¸ã¼ã§ã¯ããã®ã§ããããããæ ã«ä¼çµ±çãªHTMLã¨JavaScriptã§ã®Webéçºã¨ã¯ããªãç°ãªã£ãä¸ç観ãç解ãããã¨ãå¿ è¦ã¨ãªãã¾ãã
ãä¸æ¹ã®Svelteã§ã¯ãã³ã³ãã¼ãã³ãã®ææ¸æ§é ã¯ãã»ã¨ãã©æ®éã®HTMLã§æ¸ããã¨ãã§ãã¾ããç°ãªãã®ã¯ã{foobar}
ã§ç¤ºãããæ§æå¤æ°ãå¶å¾¡æ§é ã使ããã¨ãã§ãããã¨ã¨ãSvelteä¸ã§ã®ã¤ãã³ããã³ãã©ã表ãç¹å¥ãªå±æ§ï¼ä¸è¨ã®ä¾ã§ã¯on:click
ï¼ã使ãããã¨ãããã§ãããããã¯ä½¿ããªãã¦ãè¯ãã®ã§ãåãªãHTMLã®æçãã³ãããã¦Svelteã³ã³ãã¼ãã³ãã¨ãã¦ä½¿ãã¾ãããã¨ãã§ãã¾ãã
ãç¹ã«æ³¨ç®ãã¹ãã¯ãã³ã³ãã¼ãã³ãå
ã®ç¶æ
ï¼ã¹ãã¼ãï¼ã使ãæ¹æ³ã§ããReactã§ã¯ããããuseState
ï¼é¢æ°ã³ã³ãã¼ãã³ãã®å ´åï¼ãsetState
ï¼ã¯ã©ã¹ã³ã³ãã¼ãã³ãã®å ´åï¼ã¨ãã£ãç¹å¥ãªé¢æ°ã使ã£ã¦å®ç¾ããã¨ãããSvelteã§ã¯ãé常ã®JavaScriptã®å¤æ°ãå®ç¾©ããæ°ããå¤ã代å
¥ããã ãã§ä½¿ç¨ãããã¨ãã§ãã¾ããããã«ãã£ã¦ãç»é¢ã®è¡¨ç¤ºå
容ã®å¤åãä¼´ãåçãªå¦çã§ããæ®éã®JavaScriptãæ¸ãã®ã¨åãæè¦ã§éçºãããã¨ãã§ãã¾ãã