Site icon C1CTech

ReactJS|Creating First React Component

<p>In this article we will talk about what is component and how to create our first react component with simple example&period;<&sol;p>&NewLine;<p><em><strong><span style&equals;"color&colon; &num;0000ff&semi;">React applications are made out of <span style&equals;"color&colon; &num;000000&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">components<&sol;span>&period;Components<&sol;span> let you split the entire application into independent&comma; reusable pieces which can then be composed together to form complex UI<&sol;span><&sol;strong>&period;<&sol;em><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"91uk0jBbWOo" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;91uk0jBbWOo" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;91uk0jBbWOo&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;91uk0jBbWOo&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>What is component&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>A component is a small&comma; reusable chunk of code that is responsible for one job&period; That job is often to render some HTML&period;<&sol;p>&NewLine;<p>A React component can be of two types&period; It can be either a <strong class&equals;"markup--strong markup--p-strong"><span style&equals;"color&colon; &num;008000&semi;">function<&sol;span> <&sol;strong>component or a <span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"markup--strong markup--p-strong">class<&sol;strong><&sol;span> component&period;<&sol;p>&NewLine;<p><em>A component is a function or a Class which optionally accepts input and returns a React element&period;<&sol;em><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;003366&semi;"><strong>Functional Component<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><strong class&equals;"markup--strong markup--p-strong">A <span style&equals;"color&colon; &num;0000ff&semi;">function component<&sol;span><&sol;strong> is the simplest way to define a React component that is to write a JavaScript function&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Const MyComponent &equals; &lpar;props&rpar;&equals;&gt&semi;&lbrace;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Return&lpar;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;ElementOrComponent…&sol;&gt&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&rpar;&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&rcub;<&sol;strong><&sol;span><&sol;p>&NewLine;<p>The function component accepts a single &OpenCurlyDoubleQuote;props” &lpar;which stands for properties&rpar; object argument with data&period;<strong class&equals;"markup--strong markup--p-strong"> <&sol;strong>It returns what looks like HTML&comma; but is really a special JavaScript syntax called <strong><a class&equals;"markup--anchor markup--p-anchor" href&equals;"https&colon;&sol;&sol;facebook&period;github&period;io&sol;react&sol;docs&sol;introducing-jsx&period;html" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer" data-href&equals;"https&colon;&sol;&sol;facebook&period;github&period;io&sol;react&sol;docs&sol;introducing-jsx&period;html"><span style&equals;"color&colon; &num;008000&semi;">JSX<&sol;span><&sol;a><&sol;strong>&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;003366&semi;"><strong>Class Component<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>A <span style&equals;"color&colon; &num;0000ff&semi;"><strong class&equals;"markup--strong markup--p-strong">class component<&sol;strong><&sol;span> is a more featured way to define a React component&period; It also acts like a function that receives props&comma; but that function also considers an internal state as additional input that controls the returned JSX&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Class MyComponent extends React&period;Component&lbrace;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Render&lpar;&rpar;&lbrace;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Return&lpar;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;ElementOrComponent…&sol;&gt&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&rpar;&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&rcub;<&sol;strong><&sol;span><&sol;p>&NewLine;<div>&NewLine;<div>A class component must contain <span style&equals;"color&colon; &num;008000&semi;"><strong>render<&sol;strong><&sol;span> method and render method must contain a <strong><span style&equals;"color&colon; &num;008000&semi;">return<&sol;span><&sol;strong> statement and usually&comma; this return statement returns a <span style&equals;"color&colon; &num;008000&semi;"><strong>JSX<&sol;strong><&sol;span> expression&period;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><strong><span style&equals;"color&colon; &num;0000ff&semi;">The State and Props objects have one important difference that is inside a class component&comma; the State object can be changed while the Props object represents fixed values and cannot be changed&period;<&sol;span><&sol;strong><&sol;div>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>Given below is a simple <span style&equals;"color&colon; &num;008000&semi;"><strong>Hello<&sol;strong><&sol;span> class component returning special JSX syntax inside the render method &colon;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><img class&equals;"alignnone wp-image-942" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;03&sol;reactjsx&period;png" alt&equals;"reactjsx" width&equals;"785" height&equals;"640" &sol;><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p id&equals;"e379" class&equals;"graf graf--p graf-after--p"><strong>JSX<&sol;strong> allows us to describe our User Interfaces &lpar;UIs&rpar; in a syntax very close to the HTML that we are used to&period; It is&comma; however&comma; optional&period; React can be used without JSX&comma; as you can see as shown  below &colon;<&sol;p>&NewLine;<&sol;div>&NewLine;<div>&NewLine;<div><&sol;div>&NewLine;<div><img class&equals;"alignnone size-full wp-image-943" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;03&sol;reactelement&period;png" alt&equals;"reactelement" width&equals;"830" height&equals;"566" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p id&equals;"e379" class&equals;"graf graf--p graf-after--p">In fact&comma; React just compiles the <strong><span style&equals;"color&colon; &num;008000&semi;">JSX<&sol;span><&sol;strong> and transform it into <span style&equals;"color&colon; &num;008000&semi;"><strong>React&period;createElement&lpar;&rpar;<&sol;strong> <&sol;span>call as you can see in the above example inside the render method&comma; which is pure JavaScript&period; React use <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;babeljs&period;io&sol;">babel<&sol;a><&sol;strong><&sol;span> library to convert <strong>JSX<&sol;strong> into <strong>javascript<&sol;strong> so that browser can understand it&period;Then it works with compiled JavaScript in the browser&period;<&sol;p>&NewLine;<p><strong>React&period;createElement<&sol;strong> method creates and return <strong><span style&equals;"color&colon; &num;008000&semi;">react element<&sol;span><&sol;strong> to be render to the page &period;<em>React element is not what you see on the screen instead it describes what you want to see on the screen  or in particular it is a javascript object&period;<&sol;em><&sol;p>&NewLine;<p>for example &colon;<&sol;p>&NewLine;<p>Consider this as simple JSX code which we are returning from a component &period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<div class&equals;"gatsby-highlight" data-language&equals;"html">&NewLine;<pre class&equals;"language-html"><code><span class&equals;"token tag"><span class&equals;"token punctuation">&lt&semi;<&sol;span>div <span class&equals;"token attr-name">id<&sol;span><span class&equals;"token attr-value"><span class&equals;"token punctuation">&equals;<&sol;span><span class&equals;"token punctuation">'<&sol;span>login-btn<span class&equals;"token punctuation">'<&sol;span><&sol;span><span class&equals;"token punctuation">&gt&semi;<&sol;span><&sol;span>Login<span class&equals;"token tag"><span class&equals;"token punctuation">&lt&semi;&sol;<&sol;span>div<span class&equals;"token punctuation">&gt&semi;<&sol;span><&sol;span><&sol;code><&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>Corresponding to the above JSX code the generated React&period;createElement&lpar;&rpar; call look like this as shown below&colon;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<div class&equals;"gatsby-highlight" data-language&equals;"javascript">&NewLine;<pre class&equals;"language-javascript"><code class&equals;"language-javascript">React<span class&equals;"token punctuation">&period;<&sol;span><span class&equals;"token function">createElement<&sol;span><span class&equals;"token punctuation">&lpar;<&sol;span>&NewLine; <span class&equals;"token string">'div'<&sol;span><span class&equals;"token punctuation">&comma;<&sol;span>&NewLine; <span class&equals;"token punctuation">&lbrace;<&sol;span>id<span class&equals;"token punctuation">&colon;<&sol;span> <span class&equals;"token string">'login-btn'<&sol;span><span class&equals;"token punctuation">&rcub;<&sol;span><span class&equals;"token punctuation">&comma;<&sol;span>&NewLine; <span class&equals;"token string">'Login'<&sol;span>&NewLine;<span class&equals;"token punctuation">&rpar;<&sol;span><&sol;code><&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">createElement<&sol;span><&sol;strong> takes in three arguments&period; The first is a tag name string &lpar;div&comma; span&comma; etc&rpar;&comma; the second is any attributes you want the element to have&comma; the third is contents or the children of the element&comma; in this case the text &OpenCurlyDoubleQuote;Login”&period; The createElement invocation above is going to return an object with this shape&comma; which is a react element&period;<&sol;p>&NewLine;<div class&equals;"gatsby-highlight" data-language&equals;"javascript">&NewLine;<pre class&equals;"language-javascript"><code class&equals;"language-javascript"><span class&equals;"token punctuation">&lbrace;<&sol;span>&NewLine; type<span class&equals;"token punctuation">&colon;<&sol;span> <span class&equals;"token string">'div'<&sol;span><span class&equals;"token punctuation">&comma;<&sol;span>&NewLine; props<span class&equals;"token punctuation">&colon;<&sol;span> <span class&equals;"token punctuation">&lbrace;<&sol;span>&NewLine; children<span class&equals;"token punctuation">&colon;<&sol;span> <span class&equals;"token string">'Login'<&sol;span><span class&equals;"token punctuation">&comma;<&sol;span>&NewLine; id<span class&equals;"token punctuation">&colon;<&sol;span> <span class&equals;"token string">'login-btn'<&sol;span>&NewLine; <span class&equals;"token punctuation">&rcub;<&sol;span>&NewLine;<span class&equals;"token punctuation">&rcub;<&sol;span><&sol;code><&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<p>and when it’s rendered to the DOM &lpar;using ReactDOM&period;render&rpar;&comma; we’ll have a new DOM node that looks like this&comma;<&sol;p>&NewLine;<div class&equals;"gatsby-highlight" data-language&equals;"html">&NewLine;<pre class&equals;"language-html"><code><span class&equals;"token tag"><span class&equals;"token punctuation">&lt&semi;<&sol;span>div <span class&equals;"token attr-name">id<&sol;span><span class&equals;"token attr-value"><span class&equals;"token punctuation">&equals;<&sol;span><span class&equals;"token punctuation">'<&sol;span>login-btn<span class&equals;"token punctuation">'<&sol;span><&sol;span><span class&equals;"token punctuation">&gt&semi;<&sol;span><&sol;span>Login<span class&equals;"token tag"><span class&equals;"token punctuation">&lt&semi;&sol;<&sol;span>div<span class&equals;"token punctuation">&gt&semi;<&sol;span><&sol;span><&sol;code><&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>render method may consist of element &lpar;like &lt&semi;div&gt&semi;&comma;&lt&semi;p&gt&semi;&comma;&lt&semi;button&gt&semi; etc&rpar; or component of function or class type &lpar;like  <em>&OpenCurlyDoubleQuote;type&colon; Hello”<&sol;em> above&rpar;&period;<&sol;div>&NewLine;<div>When React sees an element with a function or class type &comma; it will then consult with that component to know which element it returns&comma; given the corresponding props&period; With that in mind&comma; at the end of this process&comma; React has a full Javascript representation of the DOM tree&comma;which we called as Virtual DOM&period;<&sol;div>&NewLine;<div>React efficiently translates it into DOM operations that it performs in the browser&period;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<h3><&sol;h3>&NewLine;<h3 id&equals;"10f1" class&equals;"graf graf--h3 graf-after--p"><span style&equals;"color&colon; &num;003366&semi;">Let’s create a React component&period;<&sol;span><&sol;h3>&NewLine;<h4><span style&equals;"color&colon; &num;003366&semi;"><strong>Functional Component<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>To create a function component&comma; define a new function&period; Let’s make that function return an HTML element &period;Below you can see simple Hello function component example returning JSX with output <strong>Hello&comma; Sara<&sol;strong> at right side&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-944" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;03&sol;funcexample&period;png" alt&equals;"funcexample" width&equals;"1238" height&equals;"640" &sol;><&sol;p>&NewLine;<&sol;div>&NewLine;<p>The props argument is an object that holds all the values that were passed to the component when it was rendered&period;<&sol;p>&NewLine;<p>Then we can access this attribute inside the component with a curly bracket for <strong><span style&equals;"color&colon; &num;008000&semi;">props&period;name<&sol;span> <&sol;strong>&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>ReactDOM&period;render&lpar;&rpar;<&sol;strong><&sol;span> comes from react-dom library we have imported&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;003366&semi;"><strong>ReactDom&period;render method accepts two arguments&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li>The first argument is which component or element needs to render in the dom&period;<&sol;li>&NewLine;<li>The second argument is where to render in the dom&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><span style&equals;"color&colon; &num;003366&semi;"><strong>Class Component<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>To create a class component firstly we have to define a class that extends <strong><span style&equals;"color&colon; &num;008000&semi;">Component<&sol;span><&sol;strong> after importing react library as shown below &colon;<&sol;p>&NewLine;<div class&equals;"gatsby-highlight" data-language&equals;"html">&NewLine;<pre class&equals;"language-html">class Person extends Component&lbrace;&rcub;<&sol;pre>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>Below is the simple Person class component example with the output at the right side&period;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><&sol;div>&NewLine;<p><img class&equals;"alignnone size-full wp-image-945" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;03&sol;componentexample&period;png" alt&equals;"componentexample" width&equals;"1291" height&equals;"741" &sol;><&sol;p>&NewLine;<p>The above <strong>Person<&sol;strong> class component consist of <span style&equals;"color&colon; &num;008000&semi;"><strong>constructor<&sol;strong><&sol;span> which is called initially&comma; only once when the instance of a component is created&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-947" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;03&sol;constr&period;png" alt&equals;"constr" width&equals;"499" height&equals;"177" &sol;><&sol;p>&NewLine;<p>Inside the constructor you must invoked super&lpar;&rpar; method with props as an argument so that we can use this&period;props in our component&period;<&sol;p>&NewLine;<p>Constructor is the only place where we can define <strong>state<&sol;strong>&period;The <strong>state<&sol;strong> contains data specific to this component that may change over time&period; The state is user-defined&comma; and it should be a plain JavaScript object&period;<&sol;p>&NewLine;<p>To change state we have to use <strong><span style&equals;"color&colon; &num;008000&semi;">setState<&sol;span><&sol;strong>&lpar;&rpar; method &period;In the above Person class component you can see when we click on plus&lpar;&plus;&rpar; button the <strong>onIncrement<&sol;strong> method will call and inside which we are incrementing our state <strong>age<&sol;strong> by one&period;<&sol;p>&NewLine;<p><strong>Thank you&period;I hope this tutorial will help you in understanding what is component &comma;its basics and how to create a component in our react application&period;<&sol;strong>&NewLine;

Exit mobile version