Site icon C1CTech

ReactJS| Introduction and Creating react app

<h3><span style&equals;"color&colon; &num;333399&semi;"><strong>What Is React&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>React is an open source <strong><span style&equals;"color&colon; &num;008000&semi;">Javascript<&sol;span><&sol;strong> library for building fast and interactive applications&period;This library is mainly used for building the UI or view of our application&period;The beauty of react is that because its just the view it doesn’t necessarily mean that it’s only for web applications&period;<&sol;p>&NewLine;<p>It can be used with mobile devices to build native apps&period;<&sol;p>&NewLine;<p>You can also build desktop apps &comma; virtual reality apps with react&period;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"OEUOndF&lowbar;WRM" title&equals;"ReactJS Tutorial For Beginners - 1 - ReactJS Introduction"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;OEUOndF&lowbar;WRM"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;OEUOndF&lowbar;WRM&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"ReactJS Tutorial For Beginners - 1 - ReactJS Introduction"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;333399&semi;"><strong>Components<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>React follows component based approach which lets you split your entire application into encapsulated parts which can then be composed together to form complex UI&period; <em><strong>In particular we can say<&sol;strong>&comma;<&sol;em> <span style&equals;"color&colon; &num;008000&semi;"><em><strong>A component is a function or a Class which optionally accepts input &lpar;as props&rpar; and returns a React element&period;<&sol;strong><&sol;em><&sol;span><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;333399&semi;"><strong>Functional Component<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&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;0000ff&semi;"><strong>Return&lpar;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&lt&semi;ElementOrComponent…&sol;&gt&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&rpar;&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&rcub;<&sol;strong><&sol;span><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;333399&semi;"><strong>Class Component<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Class MyComponent extends React&period;Component&lbrace;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Render&lpar;&rpar;&lbrace;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Return&lpar; <&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&lt&semi;ElementOrComponent…&sol;&gt&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&rpar;&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&rcub;<&sol;strong><&sol;span><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;333399&semi;"><strong>One way data flow<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>In react there is one way data flow that means data always flow from <strong>top to bottom<&sol;strong>&period; Now to understand this look at this tree like structure as shown below&period;<&sol;p>&NewLine;<p><img class&equals;" size-full wp-image-902 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;tree-2&period;png" alt&equals;"tree" width&equals;"508" height&equals;"451" &sol;><&sol;p>&NewLine;<p>In the above diagram you can see we have parent <strong>A<&sol;strong>&comma; A has children C and C has its own child F and G&period;Now consider <strong>C<&sol;strong> as a <strong>component<&sol;strong> &comma;if there is any change in this component than only its children F and G and their children J and K know about its change and the parent have no idea about its change&period;So due to this unidirectional flow in react niether parent component nor the components around it get affected by this change&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;333399&semi;"><strong>Virtual dom<&sol;strong><&sol;span><&sol;h3>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Problem with real DOM<&sol;strong><&sol;span><&sol;h4>&NewLine;<ol>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">DOM manipulation<&sol;span><&sol;strong> is the heart of the modern&comma; interactive web&period; Unfortunately&comma; accessing&comma; rendering&comma; rerendering  real DOM  is really very slow&period;<&sol;li>&NewLine;<li>Inefficient updating has become a serious problem with real DOM&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>As an example&comma; let&&num;8217&semi;s say that you have a list that contains ten items&period; On changing any of the item in the list the entire list get rerendered again and again&period; That&&num;8217&semi;s ten times more work than necessary&excl; Only one item changed&comma; but the remaining nine get rebuilt exactly how they were before&period;<&sol;p>&NewLine;<p>Rebuilding a list is no big deal to a web browser&comma; but modern websites can use huge amounts of DOM manipulation&comma;which reduces the performance of our webpage&period;<&sol;p>&NewLine;<p>To address this problem&comma; the people at React popularized something called the <strong><span style&equals;"color&colon; &num;000080&semi;">virtual DOM&period;<&sol;span><&sol;strong><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Virtual DOM<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><em> <&sol;em>In React&comma; for every <strong>DOM object<&sol;strong>&comma; there is a corresponding &&num;8220&semi;<strong>virtual DOM object<&sol;strong>&period;&&num;8221&semi; A virtual DOM object is a <em style&equals;"font-weight&colon; inherit&semi;">representation<&sol;em> of a DOM object&comma; like a lightweight copy or in particular we can say its a javascript representation of exact real DOM&period;<&sol;p>&NewLine;<p>A virtual DOM object has the same properties as a real DOM object&comma; but it lacks the real thing&&num;8217&semi;s power to directly change what&&num;8217&semi;s on the screen&period;<&sol;p>&NewLine;<p>Manipulating the DOM is slow&period; Manipulating the virtual DOM is much faster&comma; because nothing gets drawn onscreen&period; Think of manipulating the virtual DOM as editing a blueprint&comma; as opposed to moving rooms in an actual house&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>How virtual DOM works&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>This Virtual DOM works in three simple steps&period;<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>1&period;Whenever any underlying data changes&comma; the entire virtual DOM gets updated&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-899" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;vdom1&period;png" alt&equals;"vdom1" width&equals;"767" height&equals;"348" &sol;><&sol;p>&NewLine;<p>2&period;The virtual DOM gets compared to what it looked like before you updated it&period; React figures out which objects have changed&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;<p><img class&equals;"alignnone size-full wp-image-900" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;vdom2&period;png" alt&equals;"vdom2" width&equals;"686" height&equals;"332" &sol;><&sol;p>&NewLine;<p>3&period;Once the calculation is done&comma; the changed objects&comma; and the changed objects only&comma; get updated on the <strong><em>real<&sol;em> DOM<&sol;strong> and then finally changes on the real DOM cause the screen to change&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-901" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;vdom3-687635143-1551030866437&period;png" alt&equals;"vdom3" width&equals;"446" height&equals;"331" &sol;><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Rich Ecosystem<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>React work very well mainly with one thing and that is the view of our applications &period;Now what about routing &comma;http calls &comma;state management and other functionality of our applications&period;To achive this react has a really good ecosystem that works with latest javascript  technology to build fully fledged applications&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-904 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;react&period;png" alt&equals;"react" width&equals;"519" height&equals;"389" &sol;><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>npm<&sol;strong><&sol;span>&colon; You can find react package in npm&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>babel<&sol;strong><&sol;span> &colon; It converts JSX code into javascript so that browser can understand it&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>redux<&sol;strong><&sol;span> &colon; It manages the application state&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>react-router<&sol;strong><&sol;span> &colon; You can use this library for routing in our application&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Webpack<&sol;strong><&sol;span> &colon; Webpack is a module bundler&period; Its main purpose is to bundle JavaScript files for usage in a browser&comma; yet it is also capable of transforming&comma; bundling&comma; or packaging just about any resource or asset&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating React App<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"Ps8Suvf6TXI" title&equals;"ReactJS Tutorial For Beginners - 2 - Creating Hello World App"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;Ps8Suvf6TXI"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;Ps8Suvf6TXI&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"ReactJS Tutorial For Beginners - 2 - Creating Hello World App"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>To create react application firstly we have to install two things in our machine&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>1&period;nodejs<&sol;strong><&sol;span><&sol;p>&NewLine;<p>Now to install nodejs go to official website of nodejs  <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;nodejs&period;org&sol;en&sol;">https&colon;&sol;&sol;nodejs&period;org&sol;en&sol;<&sol;a><&sol;span><&sol;strong> &period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>2&period;code editor<&sol;strong><&sol;span><&sol;p>&NewLine;<p>Now to install code editor you can use any editor of your choice like <strong>visual studio code&comma; sublime&comma; atom<&sol;strong> etc&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Create-react-app<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>To create react applications we have to use create-react-app tool&period;It is a command line interface tool by using which we can quickly create and run react applications without doing any configuration&period;<&sol;p>&NewLine;<p>To run create-react-app tool simply run the command&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>npx create-react-app &lt&semi;project-name&gt&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><strong>Eg&colon; npx create-react-app hello-world   <&sol;strong><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Npx<&sol;strong><&sol;span> is npm package runner&period;Whenever we installed <span style&equals;"color&colon; &num;008000&semi;"><strong>nodejs<&sol;strong><&sol;span> in our machine <span style&equals;"color&colon; &num;008000&semi;"><strong>npm<&sol;strong><&sol;span> also get automatically installed and with npm version 5&period;2 and above comes with <strong><span style&equals;"color&colon; &num;008000&semi;">npx<&sol;span><&sol;strong> using which we can simply run create-react-app tool without having to installed it&period;<&sol;p>&NewLine;<p>After running above command will create project directory&lpar;ie&period;hello-world&rpar; inside the directory where you want to create your react project&period;<&sol;p>&NewLine;<p>Now to run the application firstly go to the project directory then write the command&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>cd &lt&semi;project-name&gt&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>npm start<&sol;strong><&sol;span><&sol;p>&NewLine;<p>This will launch a development server in the new tab of your browser at  <span style&equals;"color&colon; &num;008000&semi;"><strong>http&colon;&sol;&sol;localhost&colon;3000&sol;<&sol;strong><&sol;span> and this is a place where you find your project is up and running&period;<&sol;p>&NewLine;<p>There is one more approach that you can use to create react application and that is using <strong><span style&equals;"color&colon; &num;008000&semi;">create-react-app package<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>You have to globally install create-react-app package using <strong><span style&equals;"color&colon; &num;008000&semi;">npm<&sol;span><&sol;strong> by running the command &lpar;in terminal&rpar;&colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">npm install -g create-react-app<&sol;span><&sol;strong><&sol;p>&NewLine;<p>Then using create-react-app package you can simply create the react project by running&colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">create-react-app &lt&semi;project-name&gt&semi;<&sol;span><&sol;strong><&sol;p>&NewLine;<p>Now to run the application do the same as we do with npx&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>cd &lt&semi;project-name&gt&semi;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>npm start<&sol;strong><&sol;span><&sol;p>&NewLine;<p>I recommend you to use <span style&equals;"color&colon; &num;008000&semi;"><strong>npx<&sol;strong><&sol;span> instead of <span style&equals;"color&colon; &num;008000&semi;"><strong>npm<&sol;strong><&sol;span> because in case of npm we have to installed create-react-app package and also update it constantly but in case of npx we can easily run create-react-app tool without having to installed it&period;<&sol;p>&NewLine;<p><strong>I hope this tutorial will help you in understanding the basics of react and also how to create react application &period;<&sol;strong><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version