<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>component trong react - Thien An Blog</title>
	<atom:link href="https://thienanblog.com/tag/component-trong-react/feed/" rel="self" type="application/rss+xml" />
	<link>https://thienanblog.com/tag/component-trong-react/</link>
	<description>Javascript - PHP - SEO - Web Development</description>
	<lastBuildDate>Wed, 18 Oct 2017 03:16:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>

<image>
	<url>https://thienanblog.com/wp-content/uploads/2017/05/TAB_favicon.ico.png</url>
	<title>component trong react - Thien An Blog</title>
	<link>https://thienanblog.com/tag/component-trong-react/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Tìm hiểu Component trong ReactJS</title>
		<link>https://thienanblog.com/javascript/reactjs/tim-hieu-component-trong-reactjs/</link>
					<comments>https://thienanblog.com/javascript/reactjs/tim-hieu-component-trong-reactjs/#respond</comments>
		
		<dc:creator><![CDATA[Ân Vũ]]></dc:creator>
		<pubDate>Wed, 18 Oct 2017 03:16:08 +0000</pubDate>
				<category><![CDATA[ReactJS]]></category>
		<category><![CDATA[cách viết component reactjs]]></category>
		<category><![CDATA[component jsx]]></category>
		<category><![CDATA[component là gì]]></category>
		<category><![CDATA[component react]]></category>
		<category><![CDATA[component reactjs]]></category>
		<category><![CDATA[component trong react]]></category>
		<category><![CDATA[component trong reactjs]]></category>
		<category><![CDATA[viết component reactjs]]></category>
		<guid isPermaLink="false">https://thienanblog.com/?p=321</guid>

					<description><![CDATA[<p>Trong ReactJS, mỗi đoạn code sẽ được phân chia thành những Component không lệ thuộc lẫn nhau và có thể tái sử dụng khi cần thiết. Vì vậy trong bài&#8230;</p>
<p>The post <a href="https://thienanblog.com/javascript/reactjs/tim-hieu-component-trong-reactjs/">Tìm hiểu Component trong ReactJS</a> appeared first on <a href="https://thienanblog.com">Thien An Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Trong ReactJS, mỗi đoạn code sẽ được phân chia thành những Component không lệ thuộc lẫn nhau và có thể tái sử dụng khi cần thiết. Vì vậy trong bài viết này chúng ta sẽ tìm hiểu cách viết và sử dụng Component như thế nào trong ReactJS.</p>
<h3>Yêu cầu trong bài viết</h3>
<div>Bài viết này được viết trên nền tảng JavaScript ES6 + Babel Compiler có sẵn trong <span class="lang:default decode:true  crayon-inline ">create-react-app</span> . Vì thế, các bạn hãy tập làm quen với các cú pháp mới và &#8220;lạ&#8221; trong bài viết này.</div>
<div></div>
<h3>Cú pháp</h3>
<div>Để khởi tạo Component chúng ta sẽ có nhiều cách như sau:</div>
<ul>
<li>Dùng let, const trong JavaScript ES6 hoặc var ở các phiên bản JavaScript thấp hơn.</li>
<li>Dùng Function.</li>
<li>Dùng Class trong JavaScript ES6.</li>
</ul>
<h3>Ví dụ</h3>
<pre class="lang:default decode:true ">import React, { Component } from 'react';

// Ví dụ 1
let Tab_let = () =&gt; {
  return (
    &lt;div&gt;Khởi tạo bằng let&lt;/div&gt;
  );
};

// Ví dụ 2
const Tab_const = () =&gt; {
  return (
    &lt;div&gt;Khởi tạo bằng const&lt;/div&gt;
  );
};

// Ví dụ 3 - const hoặc let đều được
const Tab_shorthand = () =&gt; (
  &lt;div&gt;Cách tắt khi dùng Arrow Function&lt;/div&gt;
);

// Ví dụ 4
function Tab_function() {
  return (
    &lt;div&gt;Khởi tạo bằng Function&lt;/div&gt;
  );
}

// Ví dụ 5
class Tab_class extends Component {
  render() {
    return (
      &lt;div&gt;Khởi tạo bằng Class&lt;/div&gt;
    );
  }
}</pre>
<div>Qua ví dụ trên thì khi dùng let hoặc const, chúng ta cần dùng <a href="https://thienanblog.com/javascript/su-khac-biet-giua-arrow-function-va-function-thong-thuong/">Arrow Function</a> hoặc <a href="https://thienanblog.com/javascript/javascript-co-ban/bai-6-anonymous-function-va-closure/">Anonymous Function</a> để gán vào cho biến hoặc hằng. Trong khi Function có cách dùng tương tự cách định nghĩa biến và hằng thì Class lại phải kế thừa từ Class Component trong ReactJS để có thể chạy được.</div>
<div></div>
<div>Ở ví dụ 3, các bạn nên lưu ý là cú pháp dùng 2 dấu <span class="lang:default decode:true  crayon-inline">(&#8230;)</span>  này có ý nghĩa là vừa vừa <strong>return</strong> như ví dụ 2 và vừa <strong>tạo ra JSX</strong> ở bên trong nó. Chúng ta sẽ tìm hiểu sâu hơn về JSX ở những bài sau nên bài này các bạn &#8220;chịu khó&#8221; vẫn chưa hiểu nó nhé.</div>
<div></div>
<div>Tiếp tục sau khi khởi tạo, tên của biến, hằng, function và class sẽ trở thành 1 Component với HTML Tag là <span class="lang:default decode:true  crayon-inline ">&lt;tenBien /&gt;</span> . <strong>Nên nhớ là HTML Tag phải là thẻ tự đóng</strong>, còn khi nào thì nên dùng thẻ bao thì tôi sẽ nói rõ khi chúng ta tìm hiểu tới khái niệm về Props (Properties).</div>
<div></div>
<div>
<h3>Cách sử dụng</h3>
<div>Chúng ta chỉ đơn giản sử dụng tên thẻ như trước đó tôi đã đề cập vào bất kỳ vị trí nào nằm bên trong phạm vi sử dụng React. Chắc có lẽ sẽ có nhiều bạn đến khúc này sẽ bị lấn cấn đó là làm sao biết phạm vi của React là ở đâu? Và nó đã được khởi tạo như thế nào?</div>
<div></div>
<div>Quay lại bài viết trước, các bạn có nhớ rằng khi các bạn tạo dựng ReactJS bằng cách dùng <span class="lang:default decode:true  crayon-inline ">create-react-app</span> , chúng ta đã thấy qua file <span class="lang:default decode:true  crayon-inline ">index.js</span>  trong thư mục <span class="lang:default decode:true  crayon-inline ">src</span>  chứ. Vâng, chính là nó! Tôi sẽ giải thích cho các bạn qua cấu trúc file <span class="lang:default decode:true  crayon-inline ">public/index.html</span>  và <span class="lang:default decode:true  crayon-inline ">src/index.js</span>  bằng hình ảnh sau:</div>
<div><a href="https://thienanblog.com/wp-content/uploads/2017/10/1.index-html-index-js-structure.png"><img fetchpriority="high" decoding="async" class="aligncenter wp-image-322 size-full" src="https://thienanblog.com/wp-content/uploads/2017/10/1.index-html-index-js-structure.png" alt="1.index-html-index-js-structure.png" width="1279" height="826" srcset="https://thienanblog.com/wp-content/uploads/2017/10/1.index-html-index-js-structure.png 1279w, https://thienanblog.com/wp-content/uploads/2017/10/1.index-html-index-js-structure-300x194.png 300w, https://thienanblog.com/wp-content/uploads/2017/10/1.index-html-index-js-structure-768x496.png 768w, https://thienanblog.com/wp-content/uploads/2017/10/1.index-html-index-js-structure-1024x661.png 1024w" sizes="(max-width: 1279px) 100vw, 1279px" /></a></div>
<div>
<div></div>
<div>Hình ảnh trên chứa các phương thức mặc định dùng để khởi động ReactJS cũng như trỏ tới <span class="lang:default decode:true  crayon-inline ">&lt;div id=&#8221;root&#8221;&gt;&lt;/div&gt;</span>  để nhận biết đâu là vùng hoạt động của ReactJS. Tiếp tục, chúng ta quay trở lại file <span class="lang:default decode:true  crayon-inline ">App.js</span>  và thêm những Component mà chúng ta đã viết ở trên vào:</div>
</div>
</div>
<div></div>
<div><a href="https://thienanblog.com/wp-content/uploads/2017/10/2.add-to-app.png"><img decoding="async" class="aligncenter wp-image-323 size-full" src="https://thienanblog.com/wp-content/uploads/2017/10/2.add-to-app.png" alt="2.add-to-app" width="1109" height="826" srcset="https://thienanblog.com/wp-content/uploads/2017/10/2.add-to-app.png 1109w, https://thienanblog.com/wp-content/uploads/2017/10/2.add-to-app-300x223.png 300w, https://thienanblog.com/wp-content/uploads/2017/10/2.add-to-app-768x572.png 768w, https://thienanblog.com/wp-content/uploads/2017/10/2.add-to-app-1024x763.png 1024w" sizes="(max-width: 1109px) 100vw, 1109px" /></a></div>
<div></div>
<div>
<div>Và kết quả nhận được:</div>
</div>
<div></div>
<div><a href="https://thienanblog.com/wp-content/uploads/2017/10/3.result.png"><img decoding="async" class="aligncenter wp-image-324 size-full" src="https://thienanblog.com/wp-content/uploads/2017/10/3.result.png" alt="result" width="1217" height="454" srcset="https://thienanblog.com/wp-content/uploads/2017/10/3.result.png 1217w, https://thienanblog.com/wp-content/uploads/2017/10/3.result-300x112.png 300w, https://thienanblog.com/wp-content/uploads/2017/10/3.result-768x287.png 768w, https://thienanblog.com/wp-content/uploads/2017/10/3.result-1024x382.png 1024w" sizes="(max-width: 1217px) 100vw, 1217px" /></a></div>
<div>
<div></div>
<h3>Lời kết</h3>
<div>Như vậy, chúng ta đã tạo ra những Component trong ReactJS bằng nhiều cú pháp khác nhau và kết hợp với cú pháp JSX để tạo ra React Element. Vậy JSX là gì? Component chỉ có vậy thôi sao? Hãy đón xem tiếp ở phần sau các bạn nhé.</div>
</div>
<p>The post <a href="https://thienanblog.com/javascript/reactjs/tim-hieu-component-trong-reactjs/">Tìm hiểu Component trong ReactJS</a> appeared first on <a href="https://thienanblog.com">Thien An Blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thienanblog.com/javascript/reactjs/tim-hieu-component-trong-reactjs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
