<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Reza Ghorbani</title>
    <description>The latest articles on DEV Community by Reza Ghorbani (@rezaghorbani).</description>
    <link>https://dev.to/rezaghorbani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1109202%2Faefea9b6-8587-43c3-bc80-6226fcf921dc.jpg</url>
      <title>DEV Community: Reza Ghorbani</title>
      <link>https://dev.to/rezaghorbani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rezaghorbani"/>
    <language>en</language>
    <item>
      <title>Preact Setup: A Beginner's Guide</title>
      <dc:creator>Reza Ghorbani</dc:creator>
      <pubDate>Tue, 23 Apr 2024 13:39:23 +0000</pubDate>
      <link>https://dev.to/rezaghorbani/preact-setup-a-beginners-guide-118a</link>
      <guid>https://dev.to/rezaghorbani/preact-setup-a-beginners-guide-118a</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/rezaghorbani/preact-vs-react-a-comparative-guide-mbg" rel="noopener"&gt;previous post&lt;/a&gt;, we explored Preact,
    compared its features with React, and examined the pros and cons of each. Now, let’s get started with Preact's
    command-line to create our first project.&lt;/p&gt;
&lt;h2&gt;Creating a New Preact Application&lt;/h2&gt;
&lt;p&gt;It's very straightforward to set up your Preact project, and I'll show you exactly how in the following. However,
    before that, I think it's essential to understand how frameworks like Angular, Vue, React, and Preact compile and
    build web applications.&lt;/p&gt;
&lt;h3&gt;How Web Development Frameworks Function?&lt;/h3&gt;
&lt;p&gt;Web frameworks have their own syntax and code structure. For example, React uses &lt;strong&gt;JSX syntax&lt;/strong&gt; which
    allows you to write HTML and even CSS within your JavaScript code. Browsers cannot interpret this natively. So we
    need a tool to translate and compile our code into formats that browsers can read and execute, which are HTML, CSS,
    and JavaScript. Powerful tools for this include &lt;strong&gt;Webpack and Vite (which are more popular)&lt;/strong&gt;, as well
    as Parcel and Rollup. These tools are commonly referred to as &lt;strong&gt;module bundlers&lt;/strong&gt;.&lt;/p&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6aqRycMJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://rezaghorbani.dev/img/blog/module-builder-workflow.webp" alt="Module bundlers workflow" width="800" height="450"&gt;Module bundlers workflow

&lt;p&gt;Module bundlers allow us to break our application into separate, independent components/modules. They use appropriate
    compilers for each language and bundle the project into one or multiple files depending on our needs. Using a local
    development server, they can run our project, detect changes to automatically refresh the result, and help with
    debugging and developing our applications.&lt;/p&gt;
&lt;p&gt;Preact, just like React, can be used with a variety of these module bundlers.&lt;/p&gt;
&lt;h3&gt;Preact and Vite: The Ultimate Duo&lt;/h3&gt;
&lt;p&gt;By default, Preact uses Vite to set up a development environment. I'll explain more about Vite in another post, but
    for now, all you need to know is that Vite is a fast build tool designed to enhance the modern web development
    experience. It uses &lt;strong&gt;native ES modules (ESM)&lt;/strong&gt; and provides features like &lt;strong&gt;Hot Module
        Replacement (HMR)&lt;/strong&gt;, which allows you to see instant updates in the browser without a full page refresh.
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T6eT9_2O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://rezaghorbani.dev/img/blog/preact-vite.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T6eT9_2O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://rezaghorbani.dev/img/blog/preact-vite.webp" alt="The fast combination of Preact and Vite" width="800" height="450"&gt;&lt;/a&gt;The fast combination of Preact and Vite&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;Initializing Preact Using Vite&lt;/h2&gt;

&lt;p&gt;We will use Preact's default initializer, &lt;code&gt;create-preact&lt;/code&gt;, which is the &lt;strong&gt;command-line interface
        (CLI)&lt;/strong&gt; to scaffold our Preact application using Vite as the build tool.&lt;/p&gt;

&lt;p&gt;Open a terminal window on your device and navigate to the folder where you want to set up your project. Now run this
    command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm init preact&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or if you prefer &lt;code&gt;yarn&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;yarn create preact&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This command will install create-preact if it is not already installed on your device. You will then be prompted for
    the app name and some initial configuration options:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3QSZyffJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://rezaghorbani.dev/img/blog/preact-cli.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3QSZyffJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://rezaghorbani.dev/img/blog/preact-cli.webp" alt="Initializing Preact using create-preact CLI" width="800" height="495"&gt;&lt;/a&gt;Initializing Preact using create-preact CLI&lt;/p&gt;

&lt;p&gt;Now let's explian some points in that configuration:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Server-Side Generation (SSG)&lt;/strong&gt;: You can use server-side generation (SSG) alongside Preact's
        client-side rendering features. SSG allows desired content to be pre-rendered on the server, speeding up load
        times and improving &lt;strong&gt;SEO (search engine optimization)&lt;/strong&gt;.I'll allocate a post to explain how to
        implement SSG in both preact and react.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;ESLint&lt;/strong&gt;: is a tool that performs static code analysis on your JavaScript/Typescript code.
        It checks for typical issues like syntax errors, formatting issues, style violations, and potential bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Seamless React Compatibility&lt;/h2&gt;

&lt;p&gt;The good news is that the &lt;code&gt;preact/compat&lt;/code&gt; alias features have already been applied to our application
    through the initialization process. Therefore, no additional configuration is needed in our project to benefit
    Preact's compatibility with the React ecosystem. So for instance, you can
    &lt;code&gt;import {useState} from 'react'&lt;/code&gt;.As mentioned in the &lt;a href="https://dev.to/rezaghorbani/preact-vs-react-a-comparative-guide-mbg"&gt;previous post&lt;/a&gt;, this seamless compatibility means migrating the codebase to React would be
    straightforward if ever needed in the future.&lt;/p&gt;

&lt;p&gt;We now have our basic Preact project initialized and ready to start developing!&lt;/p&gt;

&lt;h2&gt;Final Thoughts&lt;/h2&gt;

&lt;p&gt;I chose not to cover creating a Preact project based on Webpack in order to keep this post short and straightforward.
    Additionally, the Preact development team recommends using Vite instead of webpack. However, if you prefer Webpack,
    you can use &lt;code&gt;preact-cli&lt;/code&gt;. This also gives you access to more &lt;a href="https://github.com/preactjs-templates" rel="noopener"&gt;templates and boilerplates&lt;/a&gt; for
    starting your project.&lt;/p&gt;

&lt;p&gt;In my next post, I'll explain how to migrate your current React project to Preact, whether it is built with Webpack
    or Vite.&lt;/p&gt;

&lt;p&gt;Thank you for your time.✌️&lt;/p&gt;



&lt;blockquote&gt;
&lt;br&gt;
    This article was originally published on &lt;a href="https://rezaghorbani.dev/blog"&gt;my blog&lt;/a&gt;. Please&lt;br&gt;
    feel free to explore more of my writings.&lt;br&gt;
&lt;/blockquote&gt;

</description>
      <category>preact</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Preact vs React: A Comparative Guide</title>
      <dc:creator>Reza Ghorbani</dc:creator>
      <pubDate>Tue, 23 Apr 2024 13:21:45 +0000</pubDate>
      <link>https://dev.to/rezaghorbani/preact-vs-react-a-comparative-guide-mbg</link>
      <guid>https://dev.to/rezaghorbani/preact-vs-react-a-comparative-guide-mbg</guid>
      <description>&lt;p&gt;In this post, we get to know more about &lt;a href="https://preactjs.com/" rel="noopener noreferrer"&gt;Preact&lt;/a&gt;,
    one of this year's trending libraries. And we'll compare it to &lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React&lt;/a&gt; to see which one suits better for our projects.&lt;/p&gt;

&lt;p&gt;Today, React is one of the most popular libraries for front-end development. It breaks web applications into multiple
    components that work independently and makes them easier to develop and maintain. Then why consider a replacement
    for it? We'll find out during these comparisons.&lt;/p&gt;

&lt;h2&gt;What is Preact?&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Preact is a fast, light, and powerful replacement for React&lt;/strong&gt; that has been able to gain the trust of
    web developers over time. It's an improvement to React, and &lt;strong&gt;it's very easy to migrate to&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;Key Features&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;JSX&lt;/strong&gt;: Both libraries use JSX syntax which allows you to write HTML inside JavaScript. This makes
        components easier to develop and maintain and makes the code more readable.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Components&lt;/strong&gt;: Both use a similar approach in terms of using components. Allowing your application
        to divide into multiple parts that are reusable and can compose with each other to create bigger complex
        components.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Life cycle&lt;/strong&gt;: They both can properly handle the life cycle of applications and you can determine
        to run specific codes in any part of the application life cycle using hooks and React/Preact built-in methods.
    &lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;State management and context&lt;/strong&gt;: With a similar system, they both allow you to dynamically change
        and render the User Interface of your application.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;SSR (Server Side Rendering)&lt;/strong&gt;: They both support SSR although they use a bit different ways to
        handle it. Either way you get all the tools you need to render the content on the server.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Development Utilities&lt;/strong&gt;: Each library provides its own set of development tools, such as
        command-line interfaces (CLIs) and browser extension developer tools. These utilities are essential for creating
        projects, configuring their environment and debugging the application.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Preact Compat (or &lt;code&gt;preact/compat&lt;/code&gt;)&lt;/strong&gt;: A useful tool I strongly recommend using
        alongside Preact. It transforms imports from &lt;code&gt;react&lt;/code&gt; or &lt;code&gt;react-dom&lt;/code&gt; to Preact, so for
        example, when you &lt;code&gt;import { useState } from 'react'&lt;/code&gt; , it actually imports it from Preact. This
        feature gives you the ability to &lt;strong&gt;use Preact without modifying your codebase&lt;/strong&gt;, which
        significantly simplifies the process of migrating projects to Preact or reverting back to React.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Main Differences&lt;/h2&gt;

&lt;p&gt;While there are many aspects that can be compared between these two libraries, to keep this post short, we will focus
    mainly on the key differences explored in the following sections.&lt;/p&gt;

&lt;h3&gt;Bundle Size Difference&lt;/h3&gt;

&lt;p&gt;React's gzipped bundle size is around 45KB. In contrast, &lt;strong&gt;the combined bundle size of Preact and Preact Compat
        is only 9KB&lt;/strong&gt;, which is remarkably smaller.&lt;/p&gt;

&lt;p&gt;This clear difference in bundle size is crucial not just for users with poor internet connections, but also for those
    with low-end devices. A smaller bundle ensures a smooth user experience across all devices, regardless of their
    hardware capabilities.&lt;/p&gt;

&lt;h3&gt;Performance&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Event Handling&lt;/strong&gt;: One of the main differences between these two libraries is their approach to
        handling events across the application. React uses &lt;strong&gt;a synthetic system to manage events&lt;/strong&gt; for each
        component. This ensures cross-browser consistency, optimal performance, and avoids event duplication during
        every render. On the other hand, Preact achieves these goals by &lt;strong&gt;utilizing the
            &lt;code&gt;addEventListener&lt;/code&gt; method&lt;/strong&gt;, which is the natural way of handling events in JavaScript.
        This approach makes Preact's application performance even better.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Bundle Size&lt;/strong&gt;: As mentioned earlier, Preact's remarkably small bundle size is another significant
        factor contributing to a smoother user experience.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;PWA (Progressive Web App)&lt;/strong&gt;: Preact is a &lt;strong&gt;PWA by default&lt;/strong&gt;, which makes it
        incredibly fast to load on subsequent reloads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's worth noting that one of React's key features for enhancing application performance and reducing initial load
    times is lazy loading libraries and components. This functionality is currently experimental in Preact, but it'll
    soon be stable and ready for production use in projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frezaghorbani.dev%2Fimg%2Fblog%2Fpreact-benchmark.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frezaghorbani.dev%2Fimg%2Fblog%2Fpreact-benchmark.webp" alt="Benchmark Results for Front-End Libraries on TodoMVC"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
        &lt;p&gt;Benchmark Results for Front-End Libraries on TodoMVC&lt;/p&gt;
&lt;br&gt;
    
    &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;Ecosystem and Community&lt;/h3&gt;

&lt;p&gt;Although the number of people using and developing Preact is increasing, &lt;strong&gt;the vastness of React's community is
        undoubtedly larger&lt;/strong&gt; and more established than its rival's. It's also worth mentioning that React's
    development team, which is Facebook, certainly provides better support and development compared to Preact.&lt;/p&gt;

&lt;p&gt;However, the good news is that thanks to Preact's compatibility with React's ecosystem, we can use almost every
    feature and library developed for React inside Preact. For example, Redux, MobX, styled-components, Material UI, and
    more.&lt;/p&gt;

&lt;p&gt;As you can see in the following chart, &lt;strong&gt;Preact's community population is rapidly growing&lt;/strong&gt;, and it's
    already one of the most downloaded libraries on NPM, despite some of these libraries being significantly older than
    Preact.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frezaghorbani.dev%2Fimg%2Fblog%2Fframework-downloads.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frezaghorbani.dev%2Fimg%2Fblog%2Fframework-downloads.webp" alt="NPM Download Stats for Front-End Frameworks in September 2023."&gt;&lt;/a&gt;&lt;br&gt;NPM Download Stats for Front-End Frameworks in September 2023.
    &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;So, React or Preact? Which one to use?&lt;/h2&gt;

&lt;p&gt;The answer to this question depends on your needs and the project you're developing.&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;React&lt;/strong&gt;: If your project requires a more flexible and complex API on a large scale, full
        compatibility with React's ecosystem is essential, and you feel a strong need for the community support offered
        by the library you use, React is a more suitable option.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Preact&lt;/strong&gt;: If speed, performance, and being lightweight are your priorities in your application,
        and being largely compatible and adaptable with React's ecosystem is sufficient for you, I recommend using
        Preact for your current and future projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In rare and specific cases, some libraries may use certain features of React that might not be supported in Preact.
    However, even in those situations, thanks to &lt;strong&gt;Preact Compat&lt;/strong&gt;, you can easily migrate back to React
    with minimal changes to your project's configurations.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Both libraries have similar syntax and architecture and are powerful options for web development. The vast population
    of React's community, Facebook's support and development, and its flexible and expanding ecosystem make it a better
    choice for large-scale projects.&lt;/p&gt;

&lt;p&gt;At the same time, Preact, with its growing compatibility with its rival's ecosystem and faster performance compared
    to React, is a suitable option for common projects.&lt;/p&gt;

&lt;p&gt;Certainly, over time, &lt;strong&gt;the distance between these two libraries will likely become shorter&lt;/strong&gt;, and
    perhaps they may even merge.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/rezaghorbani/preact-setup-a-beginners-guide-118a" rel="noopener"&gt;next post&lt;/a&gt;, we'll learn
    how to create a Preact project from scratch.&lt;/p&gt;

&lt;p&gt;Thanks for reading this article! ✌️&lt;/p&gt;



&lt;blockquote&gt;This article was originally published on &lt;a href="https://rezaghorbani.dev/blog" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;. Please feel free to explore more of my writings.&lt;br&gt;
&lt;/blockquote&gt;

</description>
      <category>preact</category>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
