<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ratchagarn Naewbuntad on Medium]]></title>
        <description><![CDATA[Stories by Ratchagarn Naewbuntad on Medium]]></description>
        <link>https://medium.com/@ratchagarn?source=rss-ce707270f3d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*4_xjXT73FJc3pHa7.jpg</url>
            <title>Stories by Ratchagarn Naewbuntad on Medium</title>
            <link>https://medium.com/@ratchagarn?source=rss-ce707270f3d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:14:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ratchagarn/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Vue 3 with Composition API]]></title>
            <link>https://medium.com/ksc-innovation/vue-3-with-composition-api-1c0674406887?source=rss-ce707270f3d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c0674406887</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[composition-api]]></category>
            <dc:creator><![CDATA[Ratchagarn Naewbuntad]]></dc:creator>
            <pubDate>Tue, 23 Mar 2021 11:58:01 GMT</pubDate>
            <atom:updated>2021-03-23T11:58:01.045Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*z9aOb1XLUNcJpFYfYj9Pxg.png" /></figure><p>ก่อนที่จะเริ่มเข้าสู่เนื้อหาของบทความนี้ ของเกริ่นนำก่อนซักหน่อย<br>ตัวผมเองนั้น ใช้งาน <strong>React</strong> มาตั้งแต่ช่วงปี 2014 และมีประสบการณ์ใช้ <strong>Vue</strong> มาบ้างเล็กน้อย ในช่วง <strong>version 2</strong></p><p>และเนื่องจากมีโอกาสได้ย้ายงานมาทำงานที่ใหม่ เลยมีโอกาสได้ลองใช้เจ้า <strong>Vue</strong> อย่างเป็นเรื่องเป็นราวซักที</p><p><strong>Vue</strong> ออก <strong>version 3</strong> ตัวเต็มมาในช่วงปลายปี 2020 ที่ผ่านมา ลองเข้าไปดูรายละเอียดของการ release version นี้ได้ที่ <a href="https://github.com/vuejs/vue-next/releases/tag/v3.0.0">https://github.com/vuejs/vue-next/releases/tag/v3.0.0</a></p><p>โดยนับจากวันที่มีโอกาสได้เริ่มลองใช้งาน <strong>Vue3</strong> ทำงานจริง มาจนถึงตอนที่เริ่มเขียนบทความนี้ ก็ประมาณ 1 เดือนโดยประมาณ ผมเลยอยากจะมาแชร์ให้ฟังกัน ว่าผมใช้ <strong>Vue3</strong> แล้วรู้สึกเป็นยังไงบ้าง</p><h3>Composition API</h3><p>โดย Highlight เด่นๆของ <strong>Vue3</strong> ที่ผมเห็นก็คือ <strong>Composition API</strong></p><p>โดยเจ้า Composition API นี่ จะทำให้เรา สามารถที่จะแยกส่วนประกอบต่างๆของ Vue ที่เราเคยใช้เขียนแยกเป็น Properties ใน Vue2 (<a href="https://v3.vuejs.org/api/options-api.html">Options API</a>)<br>ให้สามารถเขียนแยกออกมาเป็นส่วนๆได้</p><p>อย่างใน Vue2 เวลาเราเขียน component ด้วย <strong>Options API</strong> โครงสร้างของ component จะมีหน้าตาประมาณนี้</p><p><strong>Vue Component with Options API</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e38736c2abaccdf9d938e318703d2426/href">https://medium.com/media/e38736c2abaccdf9d938e318703d2426/href</a></iframe><p><strong>Vue Component with Composition API</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c894c4309f2fb2fb036d8844efdaaa8/href">https://medium.com/media/9c894c4309f2fb2fb036d8844efdaaa8/href</a></iframe><p>จากตัวอย่าง Code เราจะเห็นว่า การจะใช้ Composition API ใน Vue3 นั้น เราจะต้องเขียนอยู่ภายใต้ Property ที่ชื่อ ว่า <strong>setup </strong>และใน vue3 property methods เราไม่ต้องใช้แล้ว เราสามารถสร้าง methods function ได้ภายใน setup และส่งไปให้ template เรียกใช้งานได้ตามปกติ</p><p>โดย setup จะทำงานก่อนที่ component จะถูกสร้างขึ้นมา นั่นหมายความว่า code ภายในนี้จะถูกเรียกใช้เพียงแค่ครั้งเดียวในตอนแรกเท่านั้น<br>(ถ้าหากใครที่เคยเขียน React มาก่อน ในส่วนนี้ จะคล้ายๆ Life Cycle ของ <strong>React Class Component</strong> ที่เรียกว่า <strong>componentWillMount</strong>)</p><p>และทุกอย่างที่ถูก return ภายใต้ setup เราจะสามารถนำไปเรียกใช้ได้ภายใน <strong>template</strong></p><p>จาก code ตัวอย่างที่เราใช้ Composition API เราจะเห็นได้ว่า เราจะต้องทำการ import การทำงานต่างๆของ vue มาเรียกใช้ภายใน setup</p><pre>import { ref, computed } from &#39;vue&#39;</pre><p><strong>Ref</strong></p><p><a href="https://v3.vuejs.org/api/refs-api.html">ref</a> เป็น function ตัวนึงที่เอาไว้จัดการ state ของ vue โดยจะมี property ตัวเดียวคือ value ที่เอาใช้ get/set ค่าของ state</p><p><strong>Computed</strong></p><p><a href="https://v3.vuejs.org/api/computed-watch-api.html#computed">computed</a> นี้จะเหมือนกันกับใน vue2 เพียงแต่ถ้าเราใช้กับ compositon API ใน vue3 เราจะต้องเรียก function นี้มาใช้ต่างหาก</p><p>จาก Component ตัวอย่าง Counter ถ้าหากเราต้องการที่จะเขียน แยกส่วนของการจัดการ state count และนำไป reuse ให้กับ component อื่นๆ โดยเราจะแยกการจัดการ state ของ counter ออกไปอีกไฟล์ เพื่อที่จะสามารถ นำไป import ใช้กับ component อื่นๆ</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00e157f4c05bb20f94a78d0f5db19a1b/href">https://medium.com/media/00e157f4c05bb20f94a78d0f5db19a1b/href</a></iframe><p>โดยเจ้า useCount จะสามารถกำหนด ค่าเริ่มต้นของ count และ ค่าที่เราต้องการให้เพิ่มในแต่ละครั้งผ่านทาง initCount และ addBy</p><p>และเราจะสร้าง component ขึ้นมาใหม่ เพื่อที่จะใช้ useCount</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b64846b76cbefe1b431a39593b2bb73c/href">https://medium.com/media/b64846b76cbefe1b431a39593b2bb73c/href</a></iframe><p>หรือว่าจะนำไปสร้าง component แบบนี้ก็ได้เช่นกัน</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/363b2733824cca2fce514d156dda949a/href">https://medium.com/media/363b2733824cca2fce514d156dda949a/href</a></iframe><p>Code ทั้งหมดที่ยกตัวอย่างในที่ผ่านมา ผมทำ Code Sandbox ให้สามารถเข้าไปลองเล่นได้ที่ <a href="https://codesandbox.io/s/blog-vue3-composition-api-vue-0g8yj">https://codesandbox.io/s/blog-vue3-composition-api-vue-0g8yj</a></p><p>—</p><p>จากตัวอย่างเบื้องต้นที่ผมได้แสดงตัวอย่างการใช้งาน Composition API<br>เราจะเห็นได้ว่าจะมีวิธีการคิดและเขียนที่แตกต่างไปจาก version 2<br>ซึ่งจะส่งผลให้เราสามารถจับพวก state หรือ life cycle ต่างๆมัดรวมกัน แล้วแยกออกไปอีกไฟล์ และสามารถนำไป reuse ใช้ให้กับ component ต่างๆได้ ซึ่ง vue3 สามารถเขียนอะไรแบบนี้ได้แล้ว ก็ทำให้ผมอดที่จะนึกถึง React Hooks ไม่ได้</p><p>โดยผมจะขอยกตัวอย่างของ component <strong>IncreaseBoxDimension </strong>โดยที่จะเขียนออกมาเป็น version React</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8cc7880ae6de5688a1b494c1790a98c/href">https://medium.com/media/a8cc7880ae6de5688a1b494c1790a98c/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d09f2fee58298b53960cf28f53d95c4/href">https://medium.com/media/6d09f2fee58298b53960cf28f53d95c4/href</a></iframe><p>Code version React ผมได้ทำ Code Sandbox ให้สามารถเข้าไปลองเล่นได้ที่ <a href="https://codesandbox.io/s/blog-vue3-composition-api-react-lc77e">https://codesandbox.io/s/blog-vue3-composition-api-react-lc77e</a></p><p>จาก code ตัวอย่างที่ผมแสดงให้ดู เราจะเห็นว่าเจ้า Composition API ของ vue3 และ React Hooks นั้น ในการใช้งานพื้นฐาน มีความคล้ายกันในเรื่องของการเขียน code</p><p>จากจุดนี้ ผมรู้สึกว่ามันเป็นข้อดี ที่เริ่มมีความคล้ายกันในพื้นฐาน ส่งผลใน Learning Curve ของการเรียนรู้ JS Library ตัวใหม่ลดลง<br>ไม่ว่าจะเป็น เขียน React มาก่อน แล้วอยากลองเขียน Vue หรือว่า เขียน Vue มาก่อนแล้ว รู้สึกสนใจ React อยากลองไปเขียน React บ้าง</p><p>เช่น ในเคสของผม ที่เขียน React มาตลอดและต้องการที่จะมาใช้ Vue และถือเป็นโชคดีของผมที่ Vue3 ได้ release ตัว stable มาแล้วตอนที่ต้องการใช้<br>ทำให้ ช่วยร่นระยะเวลาในการเรียนรู้ และนำไอเดียของ React มาปรับใช้ใน Vue ได้ด้วยเช่นกัน</p><p>และในส่วนนี้ผมเองก็มีตัวอย่าง component ง่ายๆ ที่เป็น app ยอดนิยมในการนำมาใช้ยกตัวอย่าง นั่นก็คือ Todo นั่นเอง โดยผมได้ทำ Code Sandbox ไว้ 2 version คือ React และ Vue3</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F6mhhj&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F6mhhj&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F6mhhj%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/0896d1d10428cbdb4e534f123a057efd/href">https://medium.com/media/0896d1d10428cbdb4e534f123a057efd/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fkq60t&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fkq60t&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fkq60t%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/73ccd02af6ef6b37036e3034e1d3aa4c/href">https://medium.com/media/73ccd02af6ef6b37036e3034e1d3aa4c/href</a></iframe><p><strong>สรุปทิ้งท้ายบทความกันซักหน่อย</strong></p><p>หลังจากที่ได้สัมผัสกับเจ้า Vue 3 และ Composition API มาระดับนึง ผมรู้สึกพอใจกับเจ้า Vue3 ไม่น้อย<br>Directive ใน vue ช่วยอำนวยความสะดวกในการเขียน พอสมควร เช่น v-if, v-else<br>และยังรู้สึกว่า สามารถนำเอาความรู้ที่เขียน React มาประยุกต์ใช้ได้กับ<br>Composition API ได้ด้วย ทำให้รู้สึกสนุกในการเขียนมากขึ้น</p><p>และ Vue3 นี้ ก็รองรับ TypeScript ด้วยเช่นกัน<br><a href="https://v3.vuejs.org/guide/typescript-support.html">https://v3.vuejs.org/guide/typescript-support.html</a></p><p>แต่ด้วยความที่ Vue3 เพิ่งจะออก Stable version ได้ไม่นาน ก็ทำให้พวก Third Party ต่างๆ ยังไม่รองรับมากนัก อย่างเช่น UI Library ยอดนิยม <a href="https://vuetifyjs.com/en/">Vuetify</a><br>ซึ่งปัจจุบัน ตัว stable ยังรองรับ Vue2 อยู่ ส่วน การรองรับ Vue3 แบบเต็มๆ คาดว่าน่าจะมาในช่วง Q3 2021<br><a href="https://vuetifyjs.com/en/introduction/roadmap/">https://vuetifyjs.com/en/introduction/roadmap/</a></p><p>แต่ก็มี Third Party ที่รองรับ Vue3 แล้วเช่นกัน เช่น</p><ul><li><a href="https://2x.antdv.com/docs/vue/introduce">Ant Design of Vue</a> จะรองรับ Vue3 ที่ version 2</li><li><a href="https://element-plus.org/#/en-US">Element Plus</a></li><li><a href="https://vee-validate.logaretm.com/v4/">Vee Validate</a> จะรองรับ Vue3 ที่ version 4</li></ul><p>—</p><p>บทความนี้เป็นบทความแรกที่ผมเขียนขึ้นมา ถ้าหากมีข้อมูลตกหล่น<br>ผิดพลาดหรือไม่ครบถ้วน สามารถแนะนำติชม กันเข้ามาได้ทาง comments นะครับ<br><strong>ขอบคุณครับ</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c0674406887" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ksc-innovation/vue-3-with-composition-api-1c0674406887">Vue 3 with Composition API</a> was originally published in <a href="https://medium.com/ksc-innovation">Krungsri Consumer-Innovation</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>