{"id":15579,"date":"2023-01-27T07:33:40","date_gmt":"2023-01-27T13:33:40","guid":{"rendered":"https:\/\/upmostly.com\/?p=15579"},"modified":"2023-01-27T07:33:42","modified_gmt":"2023-01-27T13:33:42","slug":"how-to-set-up-a-react-application-using-vite","status":"publish","type":"post","link":"http:\/\/upmostly.com\/tutorials\/how-to-set-up-a-react-application-using-vite","title":{"rendered":"How to set up a React application using Vite"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>React is a popular JavaScript library for building user interfaces used by many developers worldwide. One of the tools that can help developers build React applications more efficiently is a build tool.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a Build tool?<\/h3>\n\n\n\n<p><strong>A build tool is a software that helps automate the process of building, testing, and deploying an application.<\/strong><\/p>\n\n\n\n<p>Several build tools are available for React, including <strong><a href=\"https:\/\/webpack.js.org\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/webpack.js.org\/\" rel=\"noreferrer noopener nofollow\">Webpack<\/a><\/strong>, <strong><a href=\"https:\/\/parceljs.org\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/parceljs.org\/\" rel=\"noreferrer noopener nofollow\">Parcel<\/a><\/strong>, and <strong><a href=\"https:\/\/rollupjs.org\/guide\/en\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/rollupjs.org\/guide\/en\/\" rel=\"noreferrer noopener nofollow\">Rollup<\/a><\/strong>.<\/p>\n\n\n\n<p>However, a newer build tool called <strong><a href=\"https:\/\/vitejs.dev\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/vitejs.dev\/\" rel=\"noreferrer noopener\">Vite<\/a><\/strong> has recently gained attention for its unique approach to building React applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Vite?<\/h2>\n\n\n\n<p><strong>Vite<\/strong>, or &#8220;<strong>Vite Is the Template Engine<\/strong>&#8220;, is a build tool designed explicitly for building modern web applications. It was developed by <strong>Evan You<\/strong>, the creator of <strong><a href=\"https:\/\/vuejs.org\/\" data-type=\"URL\" data-id=\"https:\/\/vuejs.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Vue.js<\/a><\/strong>, and was released in June 2020.<\/p>\n\n\n\n<p><strong>Vite aims to be a fast and lightweight build tool that is easy to use and configure.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What makes Vite stand out from other build tools?<\/h2>\n\n\n\n<p>There are several ways in which Vite differs from other build tools for React. Here are some of the main differences:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Native ESM imports<\/h3>\n\n\n\n<p>As mentioned earlier, Vite uses native ESM imports to bypass the bundling step. This means that <strong>Vite can serve the source files directly to the browser<\/strong>, resulting in faster build times and smaller bundle size.<\/p>\n\n\n\n<p>On the other hand, other build tools like Webpack and Parcel use a bundling process to compile the source code into a single bundle file. This can take longer, especially for large applications, and results in a larger bundle size.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Development server with hot module reloading<\/h3>\n\n\n\n<p>Vite includes a development server that automatically reloads the application when changes are made to the source code. This is known as <strong>Hot-Module Reloading<\/strong>, or <strong>Hot-Reloading<\/strong>, allowing developers to see their changes in real time without needing to refresh the browser manually.<\/p>\n\n\n\n<p>Other build tools like Webpack and Parcel also have hot module reloading, but it is not built-in and requires additional configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">JSX and TypeScript Support<\/h3>\n\n\n\n<p>Vite supports <strong>JSX<\/strong>, the syntax extension used by React to write components out of the box. This means developers can use JSX in their React applications without additional configuration.<\/p>\n\n\n\n<p>It does also support TypeScript, a popular programming language that adds optional static types to JavaScript. This makes it easier for developers to catch errors and prevent bugs in their code.<\/p>\n\n\n\n<p>Webpack and Parcel also support JSX and TypeScript; however, they require additional configuration to enable these features.<\/p>\n\n\n\n<p><em>If you haven&#8217;t yet worked with TypeScript in React, you shall not worry; we have an article that will walk you through the required steps in order to set up your React application using React. You can check it out <strong><a href=\"https:\/\/upmostly.com\/typescript\/how-to-add-typescript-to-react\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/upmostly.com\/typescript\/how-to-add-typescript-to-react\" rel=\"noreferrer noopener\">here<\/a><\/strong>.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Straightforward configuration<\/h3>\n\n\n\n<p>Vite has a more straightforward configuration than other build tools like Webpack and Parcel. This is because it uses native ESM imports and includes a development server with hot module reloading, so developers don&#8217;t have to configure these features separately.<\/p>\n\n\n\n<p>In comparison, Webpack and Parcel require more complex configurations to enable features like hot module reloading and support for JSX and TypeScript. This can be a drawback for developers who are new to building tools or want a simple and easy-to-use tool.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Vite with React<\/h2>\n\n\n\n<p>Now that we have a better understanding of what Vite is and how it differs from other build tools, let&#8217;s take a look at how we can set up our new React application.<\/p>\n\n\n\n<p>To use it with React, we first need to install the package using npm or yarn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>npm<\/strong>: <code>npm install -g vite<\/code><\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>yarn<\/strong>: <code>yarn global add vite<\/code><\/li>\n<\/ul>\n\n\n\n<p>Next, we can create a new Vite project by running the following command:<\/p>\n\n\n\n<p><code>vite create my-project<\/code><\/p>\n\n\n\n<p>This will create a new project folder called <code>my-project<\/code> with the basic files and configuration needed to start a new Vite project.<\/p>\n\n\n\n<p>Once the project is created, we can install the dependencies needed for a React application by running the following command:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>npm<\/strong>: <code>npm install react react-dom<\/code><\/li>\n\n\n\n<li><strong>yarn<\/strong>: <code>yarn add react react-dom<\/code><\/li>\n<\/ul>\n\n\n\n<p>Next, we can create a new React component by creating a file called <code>App.js<\/code> in the <code>src<\/code> directory and add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import React from 'react';\n\nconst App = () =&gt; {\n&nbsp;&nbsp;return (\n&nbsp; &nbsp;&nbsp;&lt;div&gt;\n&nbsp; &nbsp; &nbsp;&nbsp;Hello, world!\n&nbsp; &nbsp;&nbsp;&lt;\/div&gt;\n&nbsp;&nbsp;);\n}\n\nexport default App;<\/code><\/pre>\n\n\n\n<p>Finally, we can update the <code>index.html<\/code> file in the <code>public<\/code> directory to include our React component by adding the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">&lt;body&gt;\n&nbsp;&nbsp;&lt;div id=\"app\"&gt;&lt;\/div&gt;\n&nbsp;&nbsp;&lt;script type=\"module\" src=\"\/src\/App.js\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;<\/code><\/pre>\n\n\n\n<p>Now, we can start the Vite development server by running the following command:<\/p>\n\n\n\n<p><code>vite<\/code><\/p>\n\n\n\n<p>This will start the development server and open the application in the browser.<\/p>\n\n\n\n<p><strong>Any changes we make to the source code will be automatically reloaded in the browser thanks to Vite&#8217;s hot module reloading feature<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Vite is a fast and lightweight build tool specifically designed for building modern web applications. Its use of native ESM imports and a built-in development server with hot module reloading makes it a unique and efficient tool for building React applications.<\/p>\n\n\n\n<p>While it may not have all the features of other build tools like Webpack and Parcel, its simplicity and ease of use make it an excellent option for developers who want a quick and easy way to build React applications.<\/p>\n\n\n\n<p>I hope you\u2019ve enjoyed the read and it helped you gain some insight into how to setup a new React application using Vite, as well as the benefits of doing so.<\/p>\n\n\n\n<p>I&#8217;m looking forward to hearing your thoughts on this article, so feel free to leave a comment below.<\/p>\n\n\n\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction React is a popular JavaScript library for building user interfaces used by many developers worldwide. One of the tools that can help developers build React applications more efficiently is a build tool. What is a Build tool? A build tool is a software that helps automate the process of building, testing, and deploying an [&hellip;]<\/p>\n","protected":false},"author":126,"featured_media":16149,"comment_status":"open","ping_status":"open","sticky":false,"template":"post-tutorial-new.php","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-15579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"acf":[],"_links":{"self":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/15579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/users\/126"}],"replies":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/comments?post=15579"}],"version-history":[{"count":5,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/15579\/revisions"}],"predecessor-version":[{"id":16155,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/15579\/revisions\/16155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media\/16149"}],"wp:attachment":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media?parent=15579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/categories?post=15579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/tags?post=15579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}