<?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 Danh Danh on Medium]]></title>
        <description><![CDATA[Stories by Danh Danh on Medium]]></description>
        <link>https://medium.com/@danhdanh_91113?source=rss-eaf65e26be14------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WNsaBLUCpnzYWFBI</url>
            <title>Stories by Danh Danh on Medium</title>
            <link>https://medium.com/@danhdanh_91113?source=rss-eaf65e26be14------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 13:48:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danhdanh_91113/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[Tìm hiểu căn bản typescript với reactjs]]></title>
            <link>https://medium.com/@danhdanh_91113/t%C3%ACm-hi%E1%BB%83u-c%C4%83n-b%E1%BA%A3n-typescript-v%E1%BB%9Bi-reactjs-72656f8307f2?source=rss-eaf65e26be14------2</link>
            <guid isPermaLink="false">https://medium.com/p/72656f8307f2</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Danh Danh]]></dc:creator>
            <pubDate>Sat, 21 Dec 2019 14:48:35 GMT</pubDate>
            <atom:updated>2019-12-21T15:02:53.619Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VWAuA4vQExNPaa6M.png" /></figure><p>Hôm nay,mình lại một bài viết về reactjs, chủ đề hôm nay là với typescript. Những điều hay ho và những thuận lợi trong khi làm việc với nó.</p><h3><strong>I&gt; Bắt đầu:</strong></h3><p><strong>1&gt; Đôi nét về typescript:</strong></p><p>Nó do Microsoft phát triển , nó là 1 mã nguồn mở cho lập trình viên phát triển. Là hàng của ông lớn, nên chắc chắn nó phải xịn và được nhiều lập trình viên đón nhận.</p><p>Những điểm mạnh từ nó:</p><ul><li>Kế thừa các cú pháp từ javascript nên rất dễ sử dụng hơn các mã nguồn tương tự</li><li>Ngoài kế thừa từ các phiên bản javascript, nó càng hỗ trợ các cú pháp đặc trưng của hướng đối tượng như interface, class,vv nên rất mạnh mẽ khi apply các design pattern.</li><li>Được các lib/framework khuyến khích sử dụng(vì bản thân nó cũng được dần chuyển qua viết bằng typescirpt)</li><li>Những dự án lớn thành công cũng đã áp dụng typescript</li><li>Một điều nữa là nó hỗ trợ rất tốt khi dùng 1 công cụ là visual code (hàng của microsoft) được nhiều ae frontend sử dụng, hỗ trợ suggest code và báo lỗi cực mạnh khi dùng typescirpt trong công cụ này</li></ul><p>Hình dưới đây là các tool hỗ trợ typescipt</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*Ap__HM_34qu9Yed0VIaLDw.png" /></figure><p>Tham khảo thêm tại trang chủ: <a href="https://www.typescriptlang.org/">https://www.typescriptlang.org/</a></p><p><strong>2&gt; Bắt đầu cài đặt:<br></strong> Để bắt đầu làm việc với Typescript Reactjs,một cách đơn giản ta dùng luôn bộ CRA(create-react-app) nổi tiếng:</p><p>Nếu bạn dùng npm:</p><pre>npm install create-react-app<br>create-react-app &lt;name-project&gt; template --typecript</pre><p>hoặc nếu với yarn</p><pre>yarn add gloabl create-react-app<br>yarn create react-app my-app --template typescript</pre><p>hoặc theo hướng dẫn từ bài này: <a href="https://create-react-app.dev/docs/adding-typescript/">https://create-react-app.dev/docs/adding-typescript/</a></p><ul><li><strong>Kết quả:</strong></li></ul><p>Các file .js đều được viết bằng typescript</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*La20ZPKpAkBtUwTUBc3sqg.png" /><figcaption>code inside</figcaption></figure><p>Nếu bạn dùng visual code (chắc bạn nào cũng dùng), là visual code đã nhận là đang dùng Typescript (mặc định hỗ trợ, và rất mạnh)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SMil3bm0AhpSNq701_Gnow.png" /><figcaption>visual code support</figcaption></figure><p>Tiếp theo: start app:</p><pre>yarn start</pre><p>hoặc</p><pre>npm start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KUqfE-lSVsYGXozNBl4sRQ.png" /><figcaption>app running</figcaption></figure><h3><strong>II&gt; Tìm hiểu về Reacjs áp dụng typescript sẽ như thế nào:</strong></h3><p>1&gt; Cách tạo component :</p><ul><li>Với function:</li></ul><p>Bộ CRA sẽ có 1 ví dụ:</p><pre>import React from &#39;react&#39;;<br>import logo from &#39;./logo.svg&#39;;<br>import &#39;./App.css&#39;;<br><br>const App: React.FC = () =&gt; {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;header className=&quot;App-header&quot;&gt;<br>        &lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;<br>        &lt;p&gt;<br>          Edit &lt;code&gt;src/App.tsx&lt;/code&gt; and save to reload.<br>        &lt;/p&gt;<br>        &lt;a<br>          className=&quot;App-link&quot;<br>          href=&quot;https://reactjs.org&quot;<br>          target=&quot;_blank&quot;<br>          rel=&quot;noopener noreferrer&quot;<br>        &gt;<br>          Learn React<br>        &lt;/a&gt;<br>      &lt;/header&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>Qua ví dụ trên, ta thấy rằng để chỉ rõ là 1 component reactjs, ta cần thêm : React.FC sau tên component để khai báo.</p><p>Để dễ dàng tìm hiểu, tớ sẽ tạo 1 ví dụ về điều khiến tắt mở bóng đèn.Đầu tiên là component tên là SwitchButton.tsx:</p><pre>import React  from &#39;react&#39;;<br><br>const SwitchButton:React.FC = () =&gt; {<br>  return(<br>    &lt;p&gt;The light is ON&lt;/p&gt;<br>  )<br>}<br><br>export default SwitchButton;</pre><p>Lúc này tớ xóa thử đi chữ React.Fc, nó sẽ cảnh báo liền phải khai báo thêm loại cho nó</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*MTKn7CN8V50FUpMOh-GhfA.png" /></figure><p>Hay việc xóa đi return → cảnh báo rất chi tiết:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*2j7TRrcJmJ_zj4KhBqIqHg.png" /></figure><p><strong>2&gt; State:</strong></p><ul><li>Một vấn đề hay sử dụng trong khi làm việc với Reacjs, lần này tớ sẽ viết lại component SwitchButton bằng class component.</li></ul><pre>import React  from &#39;react&#39;;<br><br>interface StateButton {<br>  stateLight: boolean,<br>}<br><br>interface PropsButton {<br><br>}<br><br>export default class SwitchButton extends React.Component &lt;PropsButton,StateButton&gt; {<br>  constructor(props:PropsButton) {<br>    super(props);<br><br>    this.state = {<br>      stateLight: false,<br>    }<br>  }<br><br>  render() {<br>    return(<br>    &lt;p&gt;The light is {this.state.stateLight ? &quot;ON&quot; : &quot;OFF&quot;}&lt;/p&gt;<br>    );<br>  }<br>}</pre><ul><li>Lúc này, mình sẽ tạo cho nó 1 interface tên là StateButton, ở đây mình có thể khai báo cho nó các state có thể có trong component, ở đây mình tạo 1 state có tên là stateLight kiểu boolean chỉ trạng thái bật/mở công tắc</li><li>Sau đó bạn chú ý ở đoạn tạo component:</li></ul><pre>export default class SwitchButton extends React.Component &lt;PropsButton,StateButton&gt;</pre><p>( bạn đừng quan tâm PropsButton, ở đây mình tạo trước thui ✌)</p><p>Thế là mình có thể sử dụng như bình thường rồi đấy, giả dụ ở constructor mình set state nó kiểu khác boolen, thì điều gì xảy ra nhỉ:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*kzlLUml473D5dk5T6yDV2g.png" /><figcaption>error for type</figcaption></figure><p>Ồ, nó báo lỗi chi tiết là stateLight chỉ được kiểu boolean, không được set kiểu number! Quá tuyệt vời</p><ul><li>Giả dụ mình lại thêm 1 state mới là name mà chưa khai báo state kiểu gì ở interface:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*iQiVJGNi-7hOnNjuzGlF_w.png" /></figure><p>Báo lỗi liền, khá chặt chẽ! Tránh việc khai báo lung tung!</p><p>Qua ví dụ trên, ta có thể thấy rằng việc xây dựng reactjs với typescript rất chặt chẽ và chuyên nghiệp! Lại hỗ trợ rất tốt cho người lập trình viên trong việc cảnh báo họ, và cảnh báo rất nhanh! Tốc độ suggest code cũng nhanh hơn(có lẽ hàng của microsoft nên ăn khớp với nhanh, rất là tốt)!!</p><p><strong>2&gt; Props:</strong></p><ul><li>Thông thường ta hay dùng propstype hay flow-js, còn với typecript ta không cần phải import vào và việc hỗ trợ cũng rất tốt</li></ul><p>(Chính facebook cũng khuyêndần chuyển sang typescript là nên dùng flowjs hay các package khác)</p><ul><li>Đầu tiên, tớ đổi state quản lý tắt mở bóng đèn → props:</li></ul><pre>import React  from &#39;react&#39;;<br><br><br>interface PropsButton {<br>  stateLight: boolean,<br>}<br><br>export default class SwitchButton extends React.Component &lt;PropsButton,{}&gt; {<br><br>  render() {<br>    return(<br>    &lt;p&gt;The light is {this.props.stateLight ? &quot;ON&quot; : &quot;OFF&quot;}&lt;/p&gt;<br>    );<br>  }<br>}</pre><p>Tương tự state, ta cũng tạo ra interface để khai báo loại cho props</p><ul><li>Lần này mình sẽ tạo thêm 1 component cha tên là House → sẽ call component ButtonSwitch này:</li></ul><pre>import React  from &#39;react&#39;;<br>import SwitchButton from &#39;./SwitchButton&#39;;<br><br>const House:React.FC = () =&gt; {<br>  return(<br>    &lt;SwitchButton stateLight={false}/&gt;<br>  )<br>}<br><br>export default House;</pre><p>Ở Component House này khi mình gọi SwitchButton:</p><ul><li>props stateLight mình truyền kiểu khác boolean</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*uqRIjL9hrUXm0CtkVoKWWA.png" /></figure><ul><li>Truyền thiếu props:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*kIscTaYehj7aLFPH_6NmKA.png" /></figure><ul><li>Các trường hợp truyền dư cũng báo lỗi:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*9g8f99MdvvJc86AfNMmaKw.png" /></figure><h3><strong>III&gt; Kết luận:</strong></h3><ul><li>Typescript kết hợp với reactjs rất tốt, giúp reactjs được chặt chẽ hơn! Nó còn hỗ trợ lập trình viên và tương thích rất tốt với các editor.</li><li>Ngoài các ứng dụng trên, nó còn hỗ trợ với refs, event,… trong reacjts! hẹn sẽ viết ở bài sau!</li></ul><h3><strong>IV&gt; Tham khảo:</strong></h3><ul><li>cài đặt với create-react-app: <a href="https://create-react-app.dev/docs/adding-typescript/">https://create-react-app.dev/docs/adding-typescript/</a></li><li>trang chủ typescript: <a href="https://create-react-app.dev/docs/adding-typescript/">https://create-react-app.dev/docs/adding-typescript/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72656f8307f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Xử lý animation trong Reactjs với react-spring]]></title>
            <link>https://medium.com/@danhdanh_91113/x%E1%BB%AD-l%C3%BD-animation-trong-reactjs-v%E1%BB%9Bi-react-spring-636910f3abfa?source=rss-eaf65e26be14------2</link>
            <guid isPermaLink="false">https://medium.com/p/636910f3abfa</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-spring]]></category>
            <dc:creator><![CDATA[Danh Danh]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 13:54:30 GMT</pubDate>
            <atom:updated>2019-11-21T14:19:13.434Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*m_SwHOhwUkXxgOGG" /></figure><h3>I&gt; Giới thiệu sơ qua:</h3><ul><li>Để xử lý các animation, ta có thể lựa chọn cách viết truyền thống là css modules, viết css thuần, sau đó viết logic xử lý vài sự kiện bằng event. Tuy nhiên, ta nên thử lựa chọn 1 số thư viện, nó giúp việc quản lý animation bằng mã js dễ quản lý hơn. Trong đó 1 thư viện có thể giúp điều đó là Reacts-spring.</li><li>Nó là 1 thư viện xử lý các hoạt hình, lấy cảm hứng từ <a href="https://github.com/animatedjs/animated">animated</a> và <a href="https://github.com/chenglou/react-motion">react-motion</a>, xử lý các hoạt hình hầu như hay xảy ra với UI như việc hoạt động xử lý vật lý của con lắc lò xo khi lắc( khối lượng, độ căng của dây lò xo, ma sát với không khí). Bởi vì nó rất nhẹ ( 10.7KB với bản trên web, 9.7kb với bản trên native. Với khả năng xử lý linh động và được support từ các ông lớn như airbnb hay matterapp, thì việc được cộng đồng ủng hộ nhiệt tình từ khi nó mới ra mắt, còn chần chờ gì nữa mà không tìm hiểu nó nào.</li></ul><h3>II&gt; Cài đặt:</h3><p>Câu lệnh cài đặt quen thuộc, đơn giản với node-modules:</p><pre>npm install react-spring</pre><h3>III&gt; Tìm hiểu về api các ứng dụng hay dùng của nó:</h3><p><a href="https://www.react-spring.io/docs/hooks/api"><strong>1&gt; Các common api</strong></a><strong>:</strong></p><p>Đây là các api để config mà tất cả các spring(hook/component) đều sử dụng. Ta sử dụng chúng để config để có hoạt hình hợp lý, chia làm các nhóm sau(mình chỉnh liệt kê một số config cảm thấy hay dùng và có thể hiểu, vì nó rất nhiều).</p><ul><li>Configs:</li></ul><ol><li>duration: thời gian hiệu ứng(giá trị mặc định là undefined)</li><li>precision: độ chính xác</li><li>tension: độ căng, giá trị này càng lớn hoạt hình càng diễn ra liên tục</li><li>friction: ma sát, giá trị càng lớn dẫn đến sức cản của hoạt hình càng cao, hoạt hình diễn ra chậm hơn</li><li>mass: khối lượng</li></ol><ul><li>Presets:</li></ul><p>Đây là bộ giá trị của mass, tension và friction. Mặc định của nó là : config.default tương ứng với { mass: 1, tension: 170, friction: 26 }. Các bộ khác các bạn có thể coi trong docs của nó.</p><ul><li>Properties: Các thuộc tính hay dùng sẽ là:</li></ul><ol><li>from: (từ) cũng giống như from trong keyframe</li><li>to(đến):cũng giống như to trong keyframe</li><li>delay: tạm dừng mili giây trước khi bắt đầu</li><li>config: sử dụng các config đã nêu ở mục config trên</li><li>reset: set bằng true nếu muốn reset lại mỗi khi kết thúc 1 lần hoạt hình.</li><li>onStart: ở giai đoạn bắt đầu sẽ làm gì</li><li>onReset: ở giai đoạn đứng yên sẽ làm gì</li></ol><ul><li>Interpolations: mình thấy các api hay dùng là map, output với extrapolate.</li></ul><p>Tham khảo tại đây: <a href="https://www.react-spring.io/docs/hooks/api">https://www.react-spring.io/docs/hooks/api</a></p><p><strong>2&gt;Các hỗ trợ (unit):</strong></p><ul><li>Màu sắc(names, rgb, rgba, hsl, hsla, gradients)</li><li>Đơn vị(cm, mm, in, px, pt, pc)</li><li>Relative lengths (em, ex, ch, rem, vw, vh, vmin, vmax, %)</li><li>Góc đo(deg, rad, grad, turn)</li><li>Flex and grid units (fr, etc)</li><li>All HTML attributes</li><li>SVG paths</li><li>Arrays</li><li>String patterns (transform, border, boxShadow, etc)</li><li>Non-animatable string values (visibility, pointerEvents, etc)</li><li>ScrollTop/scrollLeft</li></ul><p><strong>3&gt; Một số hook api xử lý hoạt hình hay dùng:</strong></p><p>React-spring có 5 hook hay dùng(viết hook, nên nó được dùng như custom hook). Mình sẽ giới thiệu 1 số ví dụ cơ bản và 1 số ví dụ hay dùng của nó. Các hook sẽ dùng với các api common đã được giới thiệu trước đó.</p><p><strong>a&gt;</strong> <strong>Use Spring:</strong></p><p>Được dùng với đơn hoạt hình, để di chuyển (giống như from -&gt; to giống keyframe trong css).</p><p>Một số ví dụ:</p><ul><li>Ví dụ khi ta muốn hiệu ứng từ từ hiện ra đoạn text</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*qJIZ0CXvF5a6ANwb22I7Xw.png" /></figure><p>Ví dụ này tương đương:</p><pre>from  {<br>  opacity: 0;<br>}<br>to {<br>   opacity: 1;<br>}</pre><p>Phân tích ví dụ:</p><p>&lt;&gt; Ta sử dụng useString như một hook, và chắc chắn có truyền vào from và tham số khác và opacity , nó sẽ trả về props styles tương ứng (ngoài ra còn có set để set lại trạng thái style cho hoạt hình nếu muốn thay đổi khi có event xảy ra hay stop nếu muốn stop hoạt hình).</p><p>&lt;&gt; animation.h1 → render ra thẻ h1 + props là styles được truyền vào → animation tương ứng</p><ul><li>Ví dụ về hiệu scroll:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*js2cQH-3D8wNX-0YgB2xjw.png" /></figure><p>Lần này ta ta truyền vào thay vì param opacity , ta truyền vào scroll . Hiệu ứng này có kết quả như sau</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*VliwuJWe17qiMXeTWEIabA.png" /></figure><ul><li>Ví dụ hiệu ứng vẽ svg:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*x2smHfJpiA6JeDpnSbv3Kw.png" /></figure><p>Lần này ta thêm config delay và duration để có thể xem hiệu ứng từ từ( config là 1 hỗ trợ thêm từ react-spring nếu ta muốn hiển thị theo nhiều cách khác nhau)</p><p><strong>b) useSprings:</strong></p><p>Tương tự như với useSpring, tuy nhiên được áp dụng với 1 list các đối tượng mà ta muốn nó có hiệu ứng gần như giống nhau.</p><p><strong>c) useTrail:</strong></p><p>Nếu bạn muốn xử lý 1 mảng các phần tử, bạn muốn các phần tử trong mảng này sẽ có hiệu ứng lần lượt được thực hiện từ phần tử thứ nhất đến phần tử cuối cùng. Hiệu ứng này sẽ tạo ra hiệu ứng liên tiếp nhau, ví dụ như thế này:</p><p><a href="https://codesandbox.io/embed/zn2q57vn13">zn2q57vn13 - CodeSandbox</a></p><p><strong>d) useTransition:</strong></p><p>Nếu bạn muốn quản lý sự biến đổi của các hiệu ứng khác nhau:</p><ul><li>Ngoài các api common, nó còn còn sử dụng các api khác như from, enter , update, leave,vv để quản lý các trạng thái khi đến, rời đi hay hủy bỏ.</li><li>Ví dụ mình thấy hay nhất trong docs của nó liên quan đến chuyển trang khi kết hợp react-router:</li></ul><p><a href="https://codesandbox.io/embed/1y3yyqpq7q">1y3yyqpq7q - CodeSandbox</a></p><p><strong>e)useChain:</strong></p><p>xử dụng kết hợp với useRef để xác định đối tượng và xác định thứ tự thực hiện các animation.</p><p>Như ví dụ này:</p><p><a href="https://codesandbox.io/embed/2v716k56pr">2v716k56pr - CodeSandbox</a></p><p><strong>4&gt; Chú ý:</strong></p><p>Ngoài ra, trước đây react-spring viết thành các component xử lý animation có chức năng tương tự: Spring tương ứng với useSpring, Transition tương ứng với useTransition, Trail tương ứng với useTrail.</p><p>Nhưng giờ đa phần họ dùng với hook, các ví dụ về component trên github cũng đã đã bị xóa đi và đều chuyển sang dùng với hook.</p><p><strong>IV&gt; Kết luận:</strong></p><p>Việc sử dụng react-spring rất linh hoạt, giúp việc kiểm soát animation bằng js , nó giúp kiểm soát khi nào bắt đầu , khi nào kết thúc hay hủy bỏ hiệu ứng một cách chủ động hơn.</p><p>Ngược lại, nó có thể nặng hơn so với việc sử dụng css thuần(nhưng chỉ áp dụng cho web, và 1 số platform rất hạn chế).</p><p>Do đó, việc sử dụng animation như thế nào còn tùy thuộc mức độ của dự án, cũng như mục đích hướng tới của nó là như thế nào.</p><p><a href="https://www.react-spring.io/"><strong>Tham khảo và các animation tham khảo thêm</strong></a></p><ul><li><a href="https://www.react-spring.io/">react-spring</a></li><li><a href="http://vuilaptrinh.com/2018-04-24-huong-dan-huong-dan-lam-animation-voi-react/#nh%C6%B0%E1%BB%A3c-2">Một số cách làm Animation trong React</a></li><li><a href="https://medium.com/@dmitrynozhenko/5-ways-to-animate-a-reactjs-app-in-2019-56eb9af6e3bf">5 Ways to animate a React app in 2019.</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=636910f3abfa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Xây dựng ví dụ đơn giản về grapql với reactjs]]></title>
            <link>https://medium.com/@danhdanh_91113/x%C3%A2y-d%E1%BB%B1ng-v%C3%AD-d%E1%BB%A5-%C4%91%C6%A1n-gi%E1%BA%A3n-v%E1%BB%81-grapql-v%E1%BB%9Bi-reactjs-e06b9cb2cf08?source=rss-eaf65e26be14------2</link>
            <guid isPermaLink="false">https://medium.com/p/e06b9cb2cf08</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react-apollo]]></category>
            <category><![CDATA[apollo-graphql]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Danh Danh]]></dc:creator>
            <pubDate>Sun, 18 Aug 2019 06:13:22 GMT</pubDate>
            <atom:updated>2019-08-18T06:19:01.702Z</atom:updated>
            <content:encoded><![CDATA[<h3>Tìm hiểu căn bản về graphql(P2):Xây dựng ví dụ đơn giản về grapql và reactjs</h3><p>Bài viết này mình sẽ xây dựng server grapql: tạo bài viết mới, tạo người dùng mới → lấy thông tin bài viết/ người dùng</p><p>Sau đó dùng reactjs hiển thị thông tin người dùng(user) , ví dụ đơn giản thui!!</p><p>Hẹn bài sau mình sẽ viết về kết nối database, xác thực người dùng(login và trả về token) với prama, tạo mới,bài sau nữa là deploy!! giờ bài này dài nhác rùi</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DHY33dVE_zJgqBBO_oc7lA.png" /><figcaption>grapql server</figcaption></figure><h3><strong>Xây dựng Server Graphql</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*VOdxgwLd5P9NAONcE49lzQ.png" /><figcaption>index.js</figcaption></figure><p><strong>Đầu tiên ta sử dụng grapl-yoga để xây dựng server:</strong></p><ul><li>Ta sẽ khai báo riêng 1 file schema.grahql để khai báo các typeDefs</li><li>file resolvers sẽ chưa các resolver để xử lý</li></ul><p><strong>File schema.grahql :</strong></p><pre>type Query {<br>  user(id: ID!): User!<br>  users: [User!]!<br>  post(id: ID!): Post!<br>  posts: [Post!]!<br>}</pre><pre>type Mutation {<br>  createUser(input: UserInput!): User!,<br>  createPost(input: PostInput!): Post!,</pre><pre>}<br>input AddressInput {<br>  city: String!<br>}<br>input UserInput {<br>  name: String!<br>  company: String!<br>  website: String!<br>  email: String<br>  address: AddressInput<br>}<br>input PostInput {<br>  title: String!<br>  description: String!<br>  author: UserInput<br>  body: String!<br>}</pre><pre>type User {<br>  id: ID!<br>  name: String!<br>  company: String!<br>  address: Address!<br>  website: String!<br>  email: String<br>}</pre><pre>type Address {<br>  street: String!<br>  postalCode: String!<br>  city: String!<br>  state: String!<br>}</pre><pre>type Post {<br>  id: ID!<br>  title: String!<br>  description: String!<br>  author: User!<br>  body: String!<br>}</pre><ul><li>file này ta sẽ khai báo thông tin các query,muation,type, và các input type</li><li>ta có các type : Address lưu trữ thông tin địa chỉ của tác giả, User(tác giả) sẽ sử dụng type Address, Post(bài đăng) sử dụng type User</li></ul><p><strong>File Resolvers:</strong></p><pre>const uniqid = require(&quot;uniqid&quot;);<br>const { USERS, POSTS } = require(&quot;./data&quot;);</pre><pre>const resolvers = {<br>  Query: {<br>    user: (root, args) =&gt; USERS.find(user =&gt; user.id === args.id),<br>    users: (root, args) =&gt; USERS,<br>    post: (root, args) =&gt; POSTS.find(post =&gt; post.id === args.id),<br>    posts: (root, args) =&gt; POSTS<br>  },</pre><pre>Mutation: {<br>    createUser: (root, { input }) =&gt; {<br>      const user = {<br>        id: uniqid(),<br>        ...input<br>      };</pre><pre>USERS.push(user);<br>      return user;<br>    },<br>    createPost: (root, { input }) =&gt; {<br>      const post = {<br>        id: uniqid(),<br>        ...input<br>      };</pre><pre>POSTS.push(post);<br>      return post;<br>    }<br>  }<br>};</pre><pre>module.exports = resolvers;</pre><ul><li>Query ta sẽ có 2 loại:</li></ul><p>query tất cả tác giả users, 1 tác giả với id cụ thể user(id)</p><p>query tất cả bài đăng posts, 1 bài đăng cụ thể post(id)</p><ul><li>muation: tạo bài đăng mới(post), tác giả(user=author)</li></ul><p><strong>Truy vấn query User và Post</strong></p><ul><li>Users: tất cả người dùng</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*6VDzntgUVdBXU3Aipt274g.png" /><figcaption>query tất cả người dùng</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*Q1_RoCUecej1yShXjkoR9A.png" /><figcaption>dữ liệu trả về tất cả người dùng</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*ZLzTrNRpq4SCRlBlA3_Thg.png" /><figcaption>1 ngươi dùng cụ thể</figcaption></figure><ul><li>Tương tự, Post: allPost lấy tất cả bài đăng, post (id) lấy 1 bài đăng cụ thể</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*Lh9IzJAXQBig9-r1ESHGPg.png" /></figure><p><strong>Mutation: tạo bài đăng và người dùgn mới</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*NoZ8SKdj69nIMAl-WggaMw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*Sqj_rMJsE8hPTK2kkp8MQw.png" /><figcaption>kết quả tạo người dùgn mới</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*G7r8dm1WEjUM2_btngAkTg.png" /><figcaption>tạo bài đăng mới</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*Um57g4MFSBcle8C34wL8GQ.png" /><figcaption>thông tin bài đăng mới</figcaption></figure><h3><strong>Phần 2: Reactjs</strong></h3><p>test thử dữ liệu trả về</p><pre>import ApolloClient from &#39;apollo-boost&#39;;<br>import { gql } from &quot;apollo-boost&quot;;<br>const client = new ApolloClient({<br>uri: &#39;<a href="https://4000-b0b5c5ff-9ea1-4c4f-98aa-2838fe35095b.ws-ap0.gitpod.io/&#39;">https://4000-b0b5c5ff-9ea1-4c4f-98aa-2838fe35095b.ws-ap0.gitpod.io/&#39;</a>,<br>});</pre><pre>client</pre><pre>.query({<br>query: gql`<br>{<br>users{<br>id, name, company,website,email, address {city}<br>}<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*MKlE5TvugLLHj1N793wOsQ.png" /><figcaption>Ngon rùi!</figcaption></figure><p>Bắt đầu viết thui</p><ul><li>index.js ta khai báo AppoloClient có url của server nãy ta viết(ở đây mình đang viết online nên url ko phải local, bài sau mình sẽ viết bài về deploy server và cách cấu hình tương ứng)</li><li>Sau đó ta dùng ApploProvider với property là client đã khai báo phía trên → truyền dạng context</li><li>Chú ý cái nữa: ApploProvuder là hook → các thèn con component bên trong phải viết dạng functionless component</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*oOCwhHyoZSDkgGhxdR6syg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*X8esKmwoOHwsgQguQ5oUGg.png" /></figure><p>Component Test: hiển thị list người dùng, ở đây ta dùng useQuery, bên trong ta truỳen {} query muốn truy vấn, trả về object có data, loading,error tương ứng, việc còn lại hiển thị bằng cách dùng map() các phần tử user ra thuii!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*GpWLJGtzOTs9-_IoYavE6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*gn8ijwImGRH-68TERMlDXQ.png" /></figure><p>ôi, xong rùi!!! Xem giao diện chạy như nào nè</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rpzU5WQHjAhpcQB7U7tcOA.png" /><figcaption>kết quả</figcaption></figure><h3>Nguồn tham khảo</h3><ul><li>Workshop Chị gái xinh đẹp: <a href="https://github.com/lydiahallie/graphql-workshop">https://github.com/lydiahallie/graphql-workshop</a></li><li><a href="https://www.apollographql.com/docs/react/">https://www.apollographql.com/docs/react/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e06b9cb2cf08" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tìm hiểu về Grapql]]></title>
            <link>https://medium.com/@danhdanh_91113/t%C3%ACm-hi%E1%BB%83u-v%E1%BB%81-grapql-f4ad127d9dc1?source=rss-eaf65e26be14------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4ad127d9dc1</guid>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Danh Danh]]></dc:creator>
            <pubDate>Thu, 18 Jul 2019 06:25:18 GMT</pubDate>
            <atom:updated>2019-07-19T01:19:34.502Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bJemoNIIhii6vQ2f" /></figure><p>Mình dùng các ví dụ để giới thiệu:</p><ul><li>Rest thì mình viết python với django,python-tastypie.</li><li>Graphql thì mình applographql là chủ yếu!!</li></ul><h3>Giới thiệu sơ qua</h3><ul><li>Ngoài Reactjs, facebook còn đang phát triển 1 công nghệ mang tên graphql. Được phát triển từ năm 2012, từ lúc mình bắt đầu đọc sách,blog công nghệ thông tin đã nghe nói nhiều về nó rồi, nhưng tới mãi năm 2016 mới được facebook chính thức phát hành product. Hiện nay, số lượng dự án trên từ Âu/Mỹ 1 phần lớn dự án cũng đều đang sử dụng nó thay cho sử dụng rest để viết api cũng như các turiol rất nhiều đã thành trend đang hot.</li></ul><p>Xem thêm vidèo này: giới thiệu về sự quyết định chuyển từ Rest sang graphql</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbroQmxQAMjM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbroQmxQAMjM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbroQmxQAMjM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa826a689fe64d3e2e1728bb10cc3200/href">https://medium.com/media/aa826a689fe64d3e2e1728bb10cc3200/href</a></iframe><h3><strong>Vấn đề đặt ra, tại sao 1 số dự án lại dùng graphql?</strong></h3><p><strong>Đầu tiên, phải nói tới điểm bất lợi khi dùng Rest</strong></p><ul><li>số lượng endpoint quá nhiều , ở đây mình chỉ có 1 model</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*6obgIUfgHBsn5hu4qTMt-A.png" /><figcaption>model Post</figcaption></figure><p>Nhưng nếu mình define api, chắc cả tá api liên quan, api về crud post, api về like post, api về comment post,vv…</p><ul><li>Khi mình chỉnh sửa trong model 1 property, hoặc thêm mới → mình cần chỉnh lại trong api cả tá endpoint liên quan</li><li>Khó mà thống nhất được code khi responsive dùng Rest</li></ul><p><strong>Cách mà Graphql lấy dữ liệu về, lợi thế so với Rest</strong></p><ul><li>Ví dụ:</li></ul><p>Ta có : thông tin 1 cuốn sách được khai báo như bên dưới (schema)</p><pre>type Book {<br>    id: ID!<br>    title: String!<br>    author: String!<br>    isbn: String!<br>    url: String!<br>  }</pre><p>ta muốn query lấy thông tin sách đang có bao gồm (tiêu đề và tác giả)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*fzE33AWvaH1Z7xhWhzMFUw.png" /><figcaption>query</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*F1Ag0Yt011lJzVgPEoKV4g.png" /><figcaption>kết quả query</figcaption></figure><p>Nếu ta muốn thêm id, chỉ cần thêm trường id ở câu lệnh truy vấn → quá dễ và đơn giả, không cần phải define nhiều resource</p><ul><li>Ta cũng thấy: kết quả trả về dạng json , cũng giống như khi rest trả về</li></ul><h3><strong>1 số khái niệm căn bản của graphql</strong></h3><p><strong>Queries: sử dụng từ khóa `query`</strong></p><p>Như đã trình bày bên trên, client yêu cầu query đưa ra các yêu cầu tương ứng, thì graphql sẽ trả về tương ứng chỉ cần định nghĩa mà không cần quan tâm nó từ resource nào cả</p><p>Ngoài ra, nó còn có:</p><ul><li>Nested khi query</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*ja4Ip1qi_Zev_P_wyCYzDg.png" /><figcaption>xem demo tại trang : <a href="https://www.predic8.de/graphql-query-samples.htm">https://www.predic8.de/graphql-query-samples.htm</a></figcaption></figure><ul><li>argument</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*6AxOzHTuIVdL-pjtWQtt1g.png" /></figure><ul><li>array — association</li></ul><h4>Mutations</h4><p>Việc query cũng giống như bạn sử dụng method GET của Rest để lấy dữ liệu. thì ở đây mutation sẽ giống như để cập nhật sự thay đổi bao gồm (CRUD) tương ứng với một số method: POST PUT DELETE với Rest</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*pL80v3lbRWHB5EB124UCuQ.png" /><figcaption>viết phức tạp tùm lum thử 1 lần</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*U8isbFzBlElDb5hG-UdCyA.png" /><figcaption>tạo mới 1 quyển sách</figcaption></figure><p>Ví dụ crud product</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*PkLM7NqIVZto4QUR8BW0Fg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*F64EUqcz4rSRgKN3DIAXxg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*qrtGAOK3BIQkO6KN4tQBdw.png" /></figure><h4>Subscriptions</h4><p>Mỗi khi máy chủ cập nhật và đồng thời client đang được kết nối với máy chủ, nó sẽ lắng nghe sự kiện (realtime, kết nối thời gian thực). Khi có sự kiện cập nhật tương ứng → trả về dữ liệu cập nhật phía client tức thì</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nqzIrGmCdZDT6wPy.png" /></figure><p>Demo khác: <a href="https://github.com/nicolasdao/apollo-subscription-demo">https://github.com/nicolasdao/apollo-subscription-demo</a></p><p><strong>Resolvers:</strong></p><p>Khi mỗi request từ client gửi lên, resolers có nhiệm vụ truy vấn query để lấy dữ liệu, hay thêm sửa xóa với mutation</p><ul><li>Truy cập database</li><li>Truy cập endpoint api ngoài</li><li>Truy cập microservice</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/1*RhnK8YKyeIcPiXMsQ9HgDg.png" /><figcaption>Vẫn là ví dụ book</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/617/1*IvSLiwCstbYSgdfpT_uGrA.png" /><figcaption>ví dụ khác về lâys các channel đang có</figcaption></figure><p><strong>Schema</strong></p><p>Nó chỉ đơn giản là tập hợp các type của graphql, định nghĩa các endpoint(query, mutation, subcription)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*v4KmWBW1WAizOO4EzSH8vA.png" /><figcaption>Ví dụ về book</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/1*rIX1YloRGN8MsksWL9-0Jw.png" /><figcaption>Ví dụ khác(channel)</figcaption></figure><h3><strong>So sánh Rest với graphQl</strong></h3><p>Việc so sánh Rest với Graphql thực ra chỉ là đang tìm hiểu thế mạnh và yếu mỗi cái để có thể áp dụng vào bài tập/dự án đang có.</p><p>Graphql sinh ra cũng không có nghĩa là Rest lỗi thời và mất đi vị thế của mình, chỉ là cho ta thêm lựa chọn để công việc và học tập thêm hoàn hảo mà thui.</p><h3><strong>Tài liệu tham khảo</strong></h3><ul><li><a href="https://github.com/hemanth/graphql-demo">https://github.com/hemanth/graphql-dem</a>o → demo</li><li><a href="https://www.predic8.de/graphql-query-samples.htm">https://www.predic8.de/graphql-query-samples.htm</a> → demo mutation</li><li><a href="https://techblog.vn/tim-hieu-graphql-xay-dung-1-graphql-server-voi-prisma">https://techblog.vn/tim-hieu-graphql-xay-dung-1-graphql-server-voi-prisma</a></li><li><a href="https://techblog.vn/tim-hieu-graphql?fbclid=IwAR2sR137qjT4c9RH1jGUFyM8qFONKY12oxOTB0PWrK_sPuCIEWUsLY1O_i8">https://techblog.vn/tim-hieu-graphql</a></li><li><a href="https://l.facebook.com/l.php?u=https%3A%2F%2Fkipalog.com%2Fposts%2FSingle-endpoint-cung-GraphQL--Backend-Go%3Ffbclid%3DIwAR0bbYZVscbnKw3rYgy01rNST8vv3TFElKerYWg8BMEoKcmlm4y62qJlJnY&amp;h=AT31RMJMayZo0EFaCnP-s3vGJMNhbZ8qhQ3BurewHFT1Pz0tOJf7866M0sobBAivkUOmSOEHNElatedMzpbNRBzxpJmVnyT93Q4NMbUKZJD-NyU_ldSpniumMpe33gH4EXL-rnTDWgU9SOTnGPC-">https://kipalog.com/posts/Single-endpoint-cung-GraphQL--Backend-Go</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4ad127d9dc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tìm hiểu react hook]]></title>
            <link>https://medium.com/@danhdanh_91113/t%C3%ACm-hi%E1%BB%83u-react-hook-952028494435?source=rss-eaf65e26be14------2</link>
            <guid isPermaLink="false">https://medium.com/p/952028494435</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Danh Danh]]></dc:creator>
            <pubDate>Wed, 19 Jun 2019 16:15:50 GMT</pubDate>
            <atom:updated>2019-06-19T16:15:50.591Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qRRqwvVoPgsMRp3gLsniVg.png" /><figcaption>Tài liệu hook của React</figcaption></figure><p><em>React hook đã được lên bản chính thức kể từ phiên bản 16.8.</em> Trước đó, vào React Conf 2018 ở Cali đội ngũ phát triển đã giới thiệu nó và bắt đầu bản beta cho các phiên bản kế và nay đã là bản chính thức. Nếu bạn vào <a href="https://reactjs.org/docs/hooks-intro.html">https://reactjs.org/docs/hooks-intro.html</a> bản đã thấy nó từ beta sang new</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qRRqwvVoPgsMRp3gLsniVg.png" /></figure><p>Hook cho phép bạn không cần thiết phải tạo class component, chỉ cần tạo function less component vẫn có thể có thể sử dụng state và các features. Điều này khiến các component nhẹ nhàng hơn hơn nhiều, giảm số lượng code cần thiết phải code, đồng thời dễ hiểu ít nhầm lẫn hơn về một số khái niệm như livecycle với 1 lập trình viên mới</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fdpw9EHDh2bM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Ddpw9EHDh2bM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fdpw9EHDh2bM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad70d92540011512f84156a5f5c802a0/href">https://medium.com/media/ad70d92540011512f84156a5f5c802a0/href</a></iframe><h3>Lợi ích thực tế?Những điều mà Hook mang lại?</h3><p><strong>Giảm số lượng code đáng kể:</strong></p><ul><li>Nhờ việc quản lý state dễ dàng và hiệu quả hơn với hook, ta có thể tránh lỗi wrapper hell react như bên dưới đây</li></ul><figure><img alt="HÃ¬nh áº£nh cÃ³ liÃªn quan" src="https://cdn-images-1.medium.com/proxy/1*SU5_ws88Kh_Oio_L4Myhvg.png" /><figcaption>React Hell!!!!!</figcaption></figure><ul><li>Nhìn đoạn code không bạn cũng cảm thấy mệt mỏi với những đoạn code này, đặc biệt là đoạn code của bạn bè hay đồng nghiệp!!!!!!</li><li>So sánh từ việc không dùng hook hay có dùng hook, ta thấy số lượng code chênh lệch quá nhìu, theo như thống kê 90% với các project càng lớn</li></ul><p><strong>Hoàn toàn tự nguyện:</strong></p><p><strong>Áp dụng cả web và mobile</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FrTkkjr3s24o%3Fstart%3D2224%26feature%3Doembed%26start%3D2224&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrTkkjr3s24o&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FrTkkjr3s24o%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c331505dcd8e2047730fa91865be1b53/href">https://medium.com/media/c331505dcd8e2047730fa91865be1b53/href</a></iframe><ul><li>Việc sử dụng hook hoàn toàn do bạn và team bạn quyết định! Bạn không cần thiết phải học kiến thức mới vẫn có thể áp dụng cách viết class component với es6 từ phiên bản cũ. Hook chỉ cung cấp các cách viết ngắn gọn và tương tác dễ dàng và hiệu quả hơn với props, state, context, refs, và các lifecycle</li></ul><p><strong>UseState</strong></p><ul><li>Ví dụ về click button, tự động tăng lên. Đơn giản, không class, không wrapper. Nó chỉ đơn giản là dùng hook để quản lý login</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*07kAbcn5FOkTOAZFsquaSQ.png" /><figcaption>Ví dụ về useState</figcaption></figure><ul><li>So sánh với việc nếu ta viết component thông thường:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cs7dy-_UyKTQ9ZSwLShoOQ.png" /><figcaption>Class Component</figcaption></figure><p>Khá dài, và phải viết dạng class component , đồng thời sự kiện phải viết dạng arrow function hoặc dùng bind() .</p><p>UseEffect():</p><p>Nếu chúng ta mún cập nhật tiêu đề cho trang web ứng với số lần click</p><ul><li>Class Component với cách viết thông thường 🚕 ta bắt buột phải dùng cách viêt là class mới có các live cycle tương ứng</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*GxwbwnGxAtSSB0KHRaCUww.png" /><figcaption>LiveCycle trong Class Component</figcaption></figure><ul><li>Tuy nhiên, với hook, điều này nhẹ nhàng hẳn:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*XWz2l9LEooQCDCJwZlS0Jw.png" /><figcaption>useEffect</figcaption></figure><p>Thật đặt biệt, vẫy làm sao nó hiểu được, đồng thời nó còn có cơ chế tự hủy</p><p><strong>Những điều chú ý:</strong></p><ul><li>useState hay useEffect nằm đầu hàm và không được nằm trong các câu lệnh điều kiện if,else hay vòng lặp for, while(làm sao nó hiểu ở lần đầu render)</li><li>Bạn có thể viết tùy ý bao nhiêu cách</li></ul><p>Ngoài ra, ta còn có useContext, useReducer,…</p><h3><strong>Nguồn tham khảo:</strong></h3><ul><li>Trang chủ: <a href="https://reactjs.org/docs/hooks-intro.html">https://reactjs.org/docs/hooks-intro.html</a></li><li><a href="https://ehkoo.com/bai-viet/react-hooks-la-gi">https://ehkoo.com/bai-viet/react-hooks-la-gi</a></li><li><a href="https://medium.com/@jackyef/react-hooks-why-we-should-embrace-it-86e408663ad6">https://medium.com/@jackyef/react-hooks-why-we-should-embrace-it-86e408663ad6</a></li></ul><p><strong>Demo nhẹ:</strong> <a href="https://nluc5.codesandbox.io/">https://nluc5.codesandbox.io/</a></p><h3>Những khái niệm trong React Hook?</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=952028494435" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[. Tìm hiểu ban đầu về Styled-components]]></title>
            <link>https://medium.com/@danhdanh_91113/t%C3%ACm-hi%E1%BB%83u-ban-%C4%91%E1%BA%A7u-v%E1%BB%81-styled-components-809f5dbe05d2?source=rss-eaf65e26be14------2</link>
            <guid isPermaLink="false">https://medium.com/p/809f5dbe05d2</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[styled-components]]></category>
            <dc:creator><![CDATA[Danh Danh]]></dc:creator>
            <pubDate>Sat, 18 May 2019 14:23:19 GMT</pubDate>
            <atom:updated>2019-05-19T06:04:21.314Z</atom:updated>
            <content:encoded><![CDATA[<h3>Tại sao lại sử dụng styled component</h3><ul><li><strong><em>Cách sử dụng css, html thông thường:</em></strong></li></ul><p>Với reactjs, ta viết các style của các component như css thông thường à import file css tương ứng này vào ở component đó(hoặc root component chứa nó).</p><p>Nếu là file scss/sass ta có thể dùng node-sass để import thẳng vào hoặc có thể dùng webpack để build tương tự</p><p>Ví dụ theo cách truyền thống như dưới đây:</p><p>¨ Css</p><pre><strong>p </strong>{<br> <strong>text-align</strong>: <strong>center</strong>;<br> <strong>margin</strong>: 0 <strong>auto</strong>;<br> }</pre><p>¨ Component :</p><pre><strong>import &#39;./App.css&#39;</strong>;</pre><pre>&lt;<strong>div className=&quot;App&quot;</strong>&gt;</pre><pre>    &lt;<strong>p</strong>&gt;</pre><pre>        Edit &lt;<strong>code</strong>&gt;src/App.js&lt;/<strong>code</strong>&gt; and save to reload.</pre><pre>    &lt;/<strong>p</strong>&gt;</pre><pre>&lt;/<strong>div</strong>&gt;</pre><p>=&gt; Việc làm như thế này, không được khuyến khích vì nó không thể hiện được css nào tương ứng với component nào, style nên đi kèm với component(theo như nhà phát triển react khuyến khích)</p><p><strong><em>Cách viết style nằm trong component</em></strong></p><pre><strong>const </strong>style = {</pre><pre>    <strong>p</strong>: {</pre><pre>        <strong>textAlign</strong>: <strong>&quot;center&quot;</strong>,</pre><pre>        <strong>margin</strong>: <strong>&quot;0 auto&quot;</strong></pre><pre><strong>    </strong>}</pre><pre>}</pre><pre><strong>export class </strong>App <strong>extends </strong>React.Component {</pre><pre>    <em>render </em>= () =&gt; {</pre><pre>        <strong>return </strong>(</pre><pre>            &lt;<strong>div className=&quot;App&quot;</strong>&gt;</pre><pre>                &lt;<strong>p style=</strong>{style.<strong>p</strong>}&gt;</pre><pre>                    Edit &lt;<strong>code</strong>&gt;src/App.js&lt;/<strong>code</strong>&gt; and save to reload.</pre><pre>                &lt;/<strong>p</strong>&gt;</pre><pre>            &lt;/<strong>div</strong>&gt;</pre><pre>        )</pre><pre>            ;</pre><pre>    }</pre><pre>}</pre><p>Cách viết này tồn tại nhược điểm là style của nó phải tuân thủ theo quy tắc(babel, react) nên trông không có vẻ tự nhiên để viết lắm</p><blockquote>Chính vì điều này mà ta nên dùng 1 package bên ngoài hỗ trợ viết style nằm trong component và cú pháp gần gũi với css hơn. Ta có package styled-component có thể hỗ trợ cho cả reactjs và react-native, lượt vote trên git &gt;23k,được nhiều người tin dùng</blockquote><p>Git: <a href="https://github.com/styled-components/styled-components">https://github.com/styled-components/styled-components</a></p><p>Trang chủ: <a href="https://www.styled-components.com/">https://www.styled-components.com</a></p><h3>Tìm hiểu căn bản styled-component:</h3><h4>Cài đặt ban đầu:</h4><ul><li>Ta dùng câu lệnh npm:</li></ul><pre>npm install --save styled-components</pre><ul><li>Hoặc cdn:</li></ul><pre>&lt;script src=&quot;https://unpkg.com/styled-components/dist/styled-components.min.js&quot;&gt;&lt;/script&gt;</pre><h4>Tạo 1 component:</h4><pre><strong>import </strong><em>styled </em><strong>from &quot;styled-components&quot;</strong>;</pre><pre><strong>const </strong>component = <em>styled</em>.nameTag<strong>`</strong></pre><pre><strong>   //conent css</strong></pre><pre><strong>`</strong>;</pre><ul><li>Ví dụ: Tạo 1 component List, gồm 3 cột</li></ul><pre><strong>const </strong>Product = <em>styled</em>.div<strong>`</strong></pre><pre><strong>   height: 100px;</strong></pre><pre><strong>   background-color: #</strong> <strong>f2f4f7;</strong></pre><pre><strong>   border: solid 1px green;</strong></pre><pre><strong>`</strong>;</pre><p>Kết quả:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*btgbyXMhg8lhYgfT7SqoUA.png" /><figcaption>Gird View có 3 cột</figcaption></figure><p><strong><em>Hoặc (cách này là kế thừa component, sẽ nói sau)</em></strong></p><pre>const MyComponent = ({ className }) =&gt; (</pre><pre>&lt;div className={className}&gt;</pre><pre>&lt;span&gt;This is a span&lt;/span&gt;</pre><pre>&lt;/div&gt;</pre><pre>);</pre><pre>const MyStyledComponent = styled(MyComponent)`</pre><pre>background-color: #000;</pre><pre>&gt; span {</pre><pre>color: #fff;</pre><pre>}</pre><pre>`;</pre><h4>Cách truyền props vào 1 component</h4><ul><li>Ví dụ: tạo ra tiêu đề cho list này, ở đây thuộc tính color sẽ được truyền vào sau khi sử dụng component</li></ul><pre><strong>const </strong>Title = <em>styled</em>.h2<strong>`</strong></pre><pre><strong>   color: </strong>${props =&gt; props.<strong>color</strong>}<strong>;</strong></pre><pre><strong>   /* :default props */</strong></pre><pre><strong>   font-size: </strong>${props =&gt; props.<strong>size </strong>|| <strong>&quot;30px&quot;</strong>}<strong>;</strong></pre><pre><strong>`</strong>;</pre><p>ð Qua ví dụ, ta thấy:</p><p>¨ Title này là 1 component có thẻ h2:</p><p>¨ Thuộc tính color: props là color</p><p>¨ Thuộc tính font-size: propslà size, nhưng nó sẽ nhận giá trị 30px khi ta không truyền props vào à default props style</p><ul><li>sử dụng :</li></ul><pre>&lt;<strong>Title color=&quot;blue&quot;</strong>&gt;list product&lt;/<strong>Title</strong>&gt;</pre><p>Kết quả:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*btrQqmVxA4Q6MYwJqYG4wQ.png" /><figcaption>Title có màu xanh được ta truyền vào</figcaption></figure><h4>Kế thừa style component</h4><p>Có thể kế thừa component là styled component hoặc component bất kỳ qua cú pháp:</p><pre><strong>const </strong>extendComponent = <em>styled</em>(Component)<strong>`</strong></pre><pre><strong>   //conent</strong></pre><pre><strong>`</strong>;</pre><ul><li>Ví du: ta có component là product, và tạo ra 1 component là product speical(hiển thị đầu tiên trong danh sách) có màu nền khác các màu nền của các component còn lại:</li></ul><pre><strong>const </strong>Product = <em>styled</em>.div<strong>`</strong></pre><pre><strong>   height: 100px;</strong></pre><pre><strong>   background-color: #f2f4f7;</strong></pre><pre><strong>   border: solid 1px green;</strong></pre><pre><strong>`</strong>;</pre><pre><strong>const </strong>ProductSpecial = <em>styled</em>(Product)<strong>`</strong></pre><pre><strong>   background: darkkhaki;</strong></pre><pre><strong>`</strong>;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*3RK8XobqnY6x1HoRQBib8Q.png" /><figcaption>Product đầu tiên là SpecialProduct, nó có màu nều khác với các product còn lại, ta chỉ việc kế thừa Product và chỉ khai báo màu nền khác</figcaption></figure><h4>Pseudoelements, pseudoselectors, and nesting:</h4><p>Rất tiện lợi , ta nhúng trực tiếp vào trong nội dung css</p><ul><li>Ví dụ: khi hover vào product , ta sẽ cho nó đổ bóng</li></ul><pre><strong>const </strong>Product = <em>styled</em>.div<strong>`</strong></pre><pre><strong>   height: 100px;</strong></pre><pre><strong>   background-color: #f2f4f7;</strong></pre><pre><strong>   border: solid 1px green;</strong></pre><pre><strong>   :hover {</strong></pre><pre><strong>       box-shadow: 2px 2px 2px;</strong></pre><pre><strong>    }</strong></pre><pre><strong>`</strong>;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*nLasnA_4NtSKQk6r0YyQXw.png" /><figcaption>khi hover vào , có đổ bóng</figcaption></figure><h4>Animation:</h4><p>Để sử dụng, hơi khác với các thẻ khác, ta phải qua keyframes của nó</p><ul><li>Đầu tiên:, import sử dụng keyframes</li></ul><pre><strong>import </strong><em>styled</em>, {<em>keyframes</em>} <strong>from &quot;styled-components&quot;</strong>;</pre><ul><li>Ta tạo ra animation và sử dụng cho đối tượng image</li></ul><pre><em>/animation cho image</em></pre><pre><strong>const </strong>rotate = <em>keyframes</em><strong>`</strong></pre><pre><strong>  from {</strong></pre><pre><strong>    transform: rotate(0deg);</strong></pre><pre><strong>  }</strong></pre><pre><strong>  to {</strong></pre><pre><strong>    transform: rotate(360deg);</strong></pre><pre><strong>  }</strong></pre><pre><strong>`</strong>;</pre><pre><strong>const </strong>Image = <em>styled</em>.<strong>img`</strong></pre><pre><strong>    height: 80%;</strong></pre><pre><strong>    width:auto;</strong></pre><pre><strong>    animation: </strong>${rotate}<strong> 4s linear infinite;</strong></pre><pre><strong>`</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*CWtUgHbxlc0uMt140C9-0g.png" /><figcaption>hiệu ứng xoay tròng</figcaption></figure><p>Quay vòng vòng miết luôn~~</p><h4>Sử dụng class css đã viết sẵn:</h4><p>Ta dùng thuộc tính className cho các styled-components tương tự như các component bình thường khác, như dưới đây:</p><pre>&lt;<strong>Name className=&quot;cl-blue&quot;</strong>&gt;{products[0].<strong>name</strong>}&lt;/<strong>Name</strong>&gt;</pre><pre>Đây quả là 1 sự kết hợp tuyệt vời giữa việc viết css (nếu đã có, với những class dùng chung lặp lại) và inline style trong component</pre><h4>Với React Native:</h4><ul><li>Tương tự lun, perfect, chỉ khác component thui. Sau đây là 1 ví dụ từ trang chủ:</li></ul><pre>import React from &#39;react&#39;</pre><pre>import styled from &#39;styled-components/native&#39;</pre><pre>const StyledView = styled.View`</pre><pre>background-color: papayawhip;</pre><pre>`</pre><pre>const StyledText = styled.Text`</pre><pre>color: palevioletred;</pre><pre>`</pre><pre>class MyReactNativeComponent extends React.Component {</pre><pre>render() {</pre><pre>return (</pre><pre>&lt;StyledView&gt;</pre><pre>&lt;StyledText&gt;Hello World!&lt;/StyledText&gt;</pre><pre>&lt;/StyledView&gt;</pre><pre>)</pre><pre>}</pre><pre>}</pre><h3>Kết luận:</h3><h4>Lợi hại:</h4><p>· Style trong component,linh hoạt có thể sửa đổi dễ dàng</p><p>· Có thể dùng cho react-native</p><p>· Có thể kế thừa style của component bất kỳ/ sử dụng className</p><h4>Nhược điểm:</h4><p>· Nhiều người chưa quen với việc dùng viết css ngay trong file js</p><p>· Thuộc tính ref</p><p>· Nó sẽ generator className ngẫu nhiên, nếu ta ko có className</p><p>Với các component giống nhau, classname giống nhau, tuy nhiên việc này khá khó để mình kiểm soát</p><h3>Tham khảo:</h3><p>Trang chủ: https://www.styled-components.com/docs</p><p><a href="https://kipalog.com/posts/Gioi-thieu-co-ban-ve-Styled-components">https://kipalog.com/posts/Gioi-thieu-co-ban-ve-Styled-components</a></p><p><a href="https://luubinhan.github.io/blog/2018-07-20-huong-dan-tao-animation-cho-component-voi-react/">https://luubinhan.github.io/blog/2018-07-20-huong-dan-tao-animation-cho-component-voi-react/</a> -&gt; 1ví dụ khác khá perfect về styled-component</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=809f5dbe05d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>