{"id":12,"date":"2021-06-04T08:14:05","date_gmt":"2021-06-04T08:14:05","guid":{"rendered":"https:\/\/web-development.edusmartup.com\/?p=12"},"modified":"2021-06-04T08:14:05","modified_gmt":"2021-06-04T08:14:05","slug":"reactjs","status":"publish","type":"post","link":"https:\/\/web-development.edusmartup.com\/index.php\/2021\/06\/04\/reactjs\/","title":{"rendered":"ReactJS"},"content":{"rendered":"<p>React is a front-end library developed by Facebook. It is used for handling the view layer for web and mobile apps. ReactJS allows us to create reusable UI components. It is currently one of the most popular JavaScript libraries and has a strong foundation and large community behind it.<\/p>\n<h1>Audience<\/h1>\n<p>This tutorial will help JavaScript developers who look ahead to deal with ReactJS for the first time. We will try to introduce every concept by showing simple code examples that can be easily understood. After finishing all the chapters, you will feel confident working with ReactJS. As a bonus we will introduce additional elements that work well with ReactJS to help you learn the best practices and follow the modern JavaScript trends.<\/p>\n<h1>Prerequisites<\/h1>\n<p>If you want to work with ReactJS, you need to have solid knowledge of\u00a0<b>JavaScript, HTML5<\/b>, and\u00a0<b>CSS<\/b>. Even though ReactJS doesn&#8217;t use HTML, the JSX is similar so your HTML knowledge will be very helpful. We will explain this more in one of the next chapters. We will also use\u00a0<b>EcmaScript 2015<\/b>\u00a0syntax so any knowledge in this area can be helpful.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>ReactJS is JavaScript library used for building reusable UI components. According to React official documentation, following is the definition \u2212<\/p>\n<p>React is a library for building composable user interfaces. It encourages the creation of reusable UI components, which present data that changes over time. Lots of people use React as the V in MVC. React abstracts away the DOM from you, offering a simpler programming model and better performance. React can also render on the server using Node, and it can power native apps using React Native. React implements one-way reactive data flow, which reduces the boilerplate and is easier to reason about than traditional data binding.<\/p>\n<h2>React Features<\/h2>\n<ul class=\"list\">\n<li><b>JSX<\/b>\u00a0\u2212 JSX is JavaScript syntax extension. It isn&#8217;t necessary to use JSX in React development, but it is recommended.<\/li>\n<li><b>Components<\/b>\u00a0\u2212 React is all about components. You need to think of everything as a component. This will help you maintain the code when working on larger scale projects.<\/li>\n<li><b>Unidirectional data flow and Flux<\/b>\u00a0\u2212 React implements one-way data flow which makes it easy to reason about your app. Flux is a pattern that helps keeping your data unidirectional.<\/li>\n<li><b>License<\/b>\u00a0\u2212 React is licensed under the Facebook Inc. Documentation is licensed under CC BY 4.0.<\/li>\n<\/ul>\n<h2>React Advantages<\/h2>\n<ul class=\"list\">\n<li>Uses virtual DOM which is a JavaScript object. This will improve apps performance, since JavaScript virtual DOM is faster than the regular DOM.<\/li>\n<li>Can be used on client and server side as well as with other frameworks.<\/li>\n<li>Component and data patterns improve readability, which helps to maintain larger apps.<\/li>\n<\/ul>\n<h2>React Limitations<\/h2>\n<ul class=\"list\">\n<li>Covers only the view layer of the app, hence you still need to choose other technologies to get a complete tooling set for development.<\/li>\n<li>Uses inline templating and JSX, which might seem awkward to some developers.<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p>In this chapter, we will show you how to set up an environment for successful React development. Notice that there are many steps involved but this will help speed up the development process later. We will need\u00a0<b>NodeJS<\/b>, so if you don&#8217;t have it installed, check the link from the following table.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th width=\"12%\">Sr.No.<\/th>\n<th>Software &amp; Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>NodeJS and NPM<\/b><\/p>\n<p>NodeJS is the platform needed for the ReactJS development. Checkout our\u00a0<a href=\"http:\/\/localhost\/nodejs\/nodejs_environment_setup.htm\">NodeJS Environment Setup<\/a>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>After successfully installing NodeJS, we can start installing React upon it using npm. You can install ReactJS in two ways<\/p>\n<ul class=\"list\">\n<li>Using webpack and babel.<\/li>\n<li>Using the\u00a0<b>create-react-app<\/b>\u00a0command.<\/li>\n<\/ul>\n<h2>Installing ReactJS using webpack and babel<\/h2>\n<p><b>Webpack<\/b>\u00a0is a module bundler (manages and loads independent modules). It takes dependent modules and compiles them to a single (file) bundle. You can use this bundle while developing apps using command line or, by configuring it using webpack.config file.<\/p>\n<p>Babel is a JavaScript compiler and transpiler. It is used to convert one source code to other. Using this you will be able to use the new ES6 features in your code where, babel converts it into plain old ES5 which can be run on all browsers.<\/p>\n<h3>Step 1 &#8211; Create the Root Folder<\/h3>\n<p>Create a folder with name\u00a0<b>reactApp<\/b>\u00a0on the desktop to install all the required files, using the mkdir command.<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop&gt;mkdir reactApp\r\nC:\\Users\\username\\Desktop&gt;cd reactApp\r\n<\/pre>\n<p>To create any module, it is required to generate the\u00a0<b>package.json<\/b>\u00a0file. Therefore, after Creating the folder, we need to create a\u00a0<b>package.json<\/b>\u00a0file. To do so you need to run the\u00a0<b>npm init<\/b>\u00a0command from the command prompt.<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm init\r\n<\/pre>\n<p>This command asks information about the module such as packagename, description, author etc. you can skip these using the \u2013y option.<\/p>\n<pre class=\"prettyprint notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm init -y\r\nWrote to C:\\reactApp\\package.json:\r\n{\r\n   \"name\": \"reactApp\",\r\n   \"version\": \"1.0.0\",\r\n   \"description\": \"\",\r\n   \"main\": \"index.js\",\r\n   \"scripts\": {\r\n      \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\"\r\n   },\r\n   \"keywords\": [],\r\n   \"author\": \"\",\r\n   \"license\": \"ISC\"\r\n}\r\n<\/pre>\n<h3>Step 2 &#8211; install React and react dom<\/h3>\n<p>Since our main task is to install ReactJS, install it, and its dom packages, using\u00a0<b>install react<\/b>\u00a0and\u00a0<b>react-dom<\/b>\u00a0commands of npm respectively. You can add the packages we install, to\u00a0<b>package.json<\/b>\u00a0file using the\u00a0<b>&#8211;save<\/b>\u00a0option.<\/p>\n<pre class=\"result notranslate\">C:\\Users\\Tutorialspoint\\Desktop\\reactApp&gt;npm install react --save\r\nC:\\Users\\Tutorialspoint\\Desktop\\reactApp&gt;npm install react-dom --save\r\n<\/pre>\n<p>Or, you can install all of them in single command as \u2212<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm install react react-dom --save\r\n<\/pre>\n<h3>Step 3 &#8211; Install webpack<\/h3>\n<p>Since we are using webpack to generate bundler install webpack, webpack-dev-server and webpack-cli.<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm install webpack --save\r\nC:\\Users\\username\\Desktop\\reactApp&gt;npm install webpack-dev-server --save\r\nC:\\Users\\username\\Desktop\\reactApp&gt;npm install webpack-cli --save\r\n<\/pre>\n<p>Or, you can install all of them in single command as \u2212<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm install webpack webpack-dev-server webpack-cli --save\r\n<\/pre>\n<h3>Step 4 &#8211; Install babel<\/h3>\n<p>Install babel, and its plugins babel-core, babel-loader, babel-preset-env, babel-preset-react and, html-webpack-plugin<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm install babel-core --save-dev\r\nC:\\Users\\username\\Desktop\\reactApp&gt;npm install babel-loader --save-dev\r\nC:\\Users\\username\\Desktop\\reactApp&gt;npm install babel-preset-env --save-dev\r\nC:\\Users\\username\\Desktop\\reactApp&gt;npm install babel-preset-react --save-dev\r\nC:\\Users\\username\\Desktop\\reactApp&gt;npm install html-webpack-plugin --save-dev\r\n<\/pre>\n<p>Or, you can install all of them in single command as \u2212<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm install babel-core babel-loader babel-preset-env \r\n   babel-preset-react html-webpack-plugin --save-dev\r\n<\/pre>\n<h3>Step 5 &#8211; Create the Files<\/h3>\n<p>To complete the installation, we need to create certain files namely, index.html, App.js, main.js, webpack.config.js and, .<b><i>babelrc<\/i><\/b>. You can create these files manually or, using\u00a0<b>command prompt<\/b>.<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;type nul &gt; index.html\r\nC:\\Users\\username\\Desktop\\reactApp&gt;type nul &gt; App.js\r\nC:\\Users\\username\\Desktop\\reactApp&gt;type nul &gt; main.js\r\nC:\\Users\\username\\Desktop\\reactApp&gt;type nul &gt; webpack.config.js\r\nC:\\Users\\username\\Desktop\\reactApp&gt;type nul &gt; .babelrc\r\n<\/pre>\n<h3>Step 6 &#8211; Set Compiler, Server and Loaders<\/h3>\n<p>Open\u00a0<b>webpack-config.js<\/b>\u00a0file and add the following code. We are setting webpack entry point to be main.js. Output path is the place where bundled app will be served. We are also setting the development server to\u00a0<b>8001<\/b>\u00a0port. You can choose any port you want.<\/p>\n<p><b>webpack.config.js<\/b><\/p>\n<pre class=\"prettyprint notranslate\">const path = require('path');\r\nconst HtmlWebpackPlugin = require('html-webpack-plugin');\r\n\r\nmodule.exports = {\r\n   entry: '.\/main.js',\r\n   output: {\r\n      path: path.join(__dirname, '\/bundle'),\r\n      filename: 'index_bundle.js'\r\n   },\r\n   devServer: {\r\n      inline: true,\r\n      port: 8001\r\n   },\r\n   module: {\r\n      rules: [\r\n         {\r\n            test: \/\\.jsx?$\/,\r\n            exclude: \/node_modules\/,\r\n            loader: 'babel-loader',\r\n            query: {\r\n               presets: ['es2015', 'react']\r\n            }\r\n         }\r\n      ]\r\n   },\r\n   plugins:[\r\n      new HtmlWebpackPlugin({\r\n         template: '.\/index.html'\r\n      })\r\n   ]\r\n}\r\n<\/pre>\n<p>Open the\u00a0<b>package.json<\/b>\u00a0and delete\u00a0<b>&#8220;test&#8221; &#8220;echo \\&#8221;Error: no test specified\\&#8221; &amp;&amp; exit 1&#8243;<\/b>\u00a0inside\u00a0<b>&#8220;scripts&#8221;<\/b>\u00a0object. We are deleting this line since we will not do any testing in this tutorial. Let&#8217;s add the\u00a0<b>start<\/b>\u00a0and\u00a0<b>build<\/b>\u00a0commands instead.<\/p>\n<pre class=\"result notranslate\">\"start\": \"webpack-dev-server --mode development --open --hot\",\r\n\"build\": \"webpack --mode production\"\r\n<\/pre>\n<h3>Step 7 &#8211; index.html<\/h3>\n<p>This is just regular HTML. We are setting\u00a0<b>div id = &#8220;app&#8221;<\/b>\u00a0as a root element for our app and adding\u00a0<b>index_bundle.js<\/b>\u00a0script, which is our bundled app file.<\/p>\n<pre class=\"prettyprint notranslate\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang = \"en\"&gt;\r\n   &lt;head&gt;\r\n      &lt;meta charset = \"UTF-8\"&gt;\r\n      &lt;title&gt;React App&lt;\/title&gt;\r\n   &lt;\/head&gt;\r\n   &lt;body&gt;\r\n      &lt;div id = \"app\"&gt;&lt;\/div&gt;\r\n      &lt;script src = 'index_bundle.js'&gt;&lt;\/script&gt;\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3>Step 8 \u2212 App.jsx and main.js<\/h3>\n<p>This is the first React component. We will explain React components in depth in a subsequent chapter. This component will render\u00a0<b>Hello World<\/b>.<\/p>\n<p><b>App.js<\/b><\/p>\n<pre class=\"prettyprint notranslate\">import React, { Component } from 'react';\r\nclass App extends Component{\r\n   render(){\r\n      return(\r\n         &lt;div&gt;\r\n            &lt;h1&gt;Hello World&lt;\/h1&gt;\r\n         &lt;\/div&gt;\r\n      );\r\n   }\r\n}\r\nexport default App;\r\n<\/pre>\n<p>We need to import this component and render it to our root\u00a0<b>App<\/b>\u00a0element, so we can see it in the browser.<\/p>\n<p><b>main.js<\/b><\/p>\n<pre class=\"prettyprint notranslate\">import React from 'react';\r\nimport ReactDOM from 'react-dom';\r\nimport App from '.\/App.js';\r\n\r\nReactDOM.render(&lt;App \/&gt;, document.getElementById('app'));\r\n<\/pre>\n<p><b>Note<\/b>\u00a0\u2212 Whenever you want to use something, you need to\u00a0<b>import<\/b>\u00a0it first. If you want to make the component usable in other parts of the app, you need to\u00a0<b>export<\/b>\u00a0it after creation and import it in the file where you want to use it.<\/p>\n<p>Create a file with name\u00a0<b>.babelrc<\/b>\u00a0and copy the following content to it.<\/p>\n<pre class=\"result notranslate\">{\r\n   \"presets\":[\"env\", \"react\"]\r\n}\r\n<\/pre>\n<h3>Step 9 &#8211; Running the Server<\/h3>\n<p>The setup is complete and we can start the server by running the following command.<\/p>\n<pre class=\"result notranslate\">C:\\Users\\username\\Desktop\\reactApp&gt;npm start\r\n<\/pre>\n<p>It will show the port we need to open in the browser. In our case, it is\u00a0<b>http:\/\/localhost:8001\/<\/b>. After we open it, we will see the following output.<\/p>\n<p><img src=\"https:\/\/www.tutorialspoint.com\/reactjs\/images\/running_the_server.jpg\" alt=\"Running the Server\" \/><\/p>\n<h3>Step 10 &#8211; Generating the bundle<\/h3>\n<p>Finally, to generate the bundle you need to run the build command in the command prompt as \u2212<\/p>\n<pre class=\"result notranslate\">C:\\Users\\Tutorialspoint\\Desktop\\reactApp&gt;npm run build\r\n<\/pre>\n<p>This will generate the bundle in the current folder as shown below.<\/p>\n<p><img src=\"https:\/\/www.tutorialspoint.com\/reactjs\/images\/generating_the_bundle.jpg\" alt=\"Generating the bundle\" \/><\/p>\n<h2>Using the create-react-app command<\/h2>\n<p>Instead of using webpack and babel you can install ReactJS more simply by installing\u00a0<b>create-react-app<\/b>.<\/p>\n<h3>Step 1 &#8211; install create-react-app<\/h3>\n<p>Browse through the desktop and install the Create React App using command prompt as shown below \u2212<\/p>\n<pre class=\"result notranslate\">C:\\Users\\Tutorialspoint&gt;cd C:\\Users\\Tutorialspoint\\Desktop\\\r\nC:\\Users\\Tutorialspoint\\Desktop&gt;npx create-react-app my-app\r\n<\/pre>\n<p>This will create a folder named my-app on the desktop and installs all the required files in it.<\/p>\n<h3>Step 2 &#8211; Delete all the source files<\/h3>\n<p>Browse through the src folder in the generated my-app folder and remove all the files in it as shown below \u2212<\/p>\n<pre class=\"result notranslate\">C:\\Users\\Tutorialspoint\\Desktop&gt;cd my-app\/src\r\nC:\\Users\\Tutorialspoint\\Desktop\\my-app\\src&gt;del *\r\nC:\\Users\\Tutorialspoint\\Desktop\\my-app\\src\\*, Are you sure (Y\/N)? y\r\n<\/pre>\n<h3>Step 3 &#8211; Add files<\/h3>\n<p>Add files with names\u00a0<b>index.css<\/b>\u00a0and\u00a0<b>index.js<\/b>\u00a0in the src folder as \u2212<\/p>\n<pre class=\"result notranslate\">C:\\Users\\Tutorialspoint\\Desktop\\my-app\\src&gt;type nul &gt; index.css\r\nC:\\Users\\Tutorialspoint\\Desktop\\my-app\\src&gt;type nul &gt; index.js\r\n<\/pre>\n<p>In the index.js file add the following code<\/p>\n<pre class=\"result notranslate\">import React from 'react';\r\nimport ReactDOM from 'react-dom';\r\nimport '.\/index.css';\r\n<\/pre>\n<h3>Step 4 &#8211; Run the project<\/h3>\n<p>Finally, run the project using the start command.<\/p>\n<pre class=\"result notranslate\">npm start\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img src=\"https:\/\/www.tutorialspoint.com\/reactjs\/images\/run_the_project.jpg\" alt=\"Run the Project\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is a front-end library developed by Facebook. It is used for handling the view layer for web and mobile apps. ReactJS allows us to create reusable UI components. It is currently one of the most popular JavaScript libraries and has a strong foundation and large community behind it. Audience This tutorial will help JavaScript &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/web-development.edusmartup.com\/index.php\/2021\/06\/04\/reactjs\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;ReactJS&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/posts\/12"}],"collection":[{"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":2,"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":14,"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/posts\/12\/revisions\/14"}],"wp:attachment":[{"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web-development.edusmartup.com\/index.php\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}