React, the most popular front-end framework nowadays, and for good reasons, let alone its virtual DOM, one-way data binding, simplicity, and several other amazing features.
Preact, on the other hand, is a clever approach to come around with a tiny core-value proposition of React, having the same API composition, but way faster and easier than React.
“The objective behind developing PreactJS is to offer memory efficiency, better performance, and closer compatibility with React.”
Over the course of a few years, a wide spectrum of websites from small open-source projects to the large MNCs have managed to switch to Preact, that includes Google, Uber, Pepsi, Financial Times, Tencent QQ, Housing.com, The New York Times, Dailymotion, Groupon, and the list goes on and on.
Here, we’ll discuss a few things about Preact-
Preact is a JavaScript library, known as _“Fast 3kb React Alternative with Same ES6 API” _or ‘Drop-in replacement for React’, created by Jason Miller. The most interesting thing about this library is its small size. The particular ‘3kb alternative’ term is used to indicate its 3kb size.
Regardless of its smaller size, it claims to perform better than React. It is a React clone library that is identical to React in terms of modern API, Components & Virtual DOM. Swapping React with Preact is very easy and that too without worrying about performance attenuation.
You would be surprised to see how easy it is to use Preact. All you have to do is to install Preact and preact-compat and alias this preact-compat in your code with React. (Here, compat stands for compatibility)
“preact-compat is a 2kb compatibility layer that allows you to leverage the many libraries of the React ecosystem and use them with Preact.”
This aliasing enables the React ecosystem to be used in your application. You can import and use react modules & libraries, as many as you want, in your Preact project. That’s incredible, isn’t it??
Here’s a small list of features that Preact has in-store for you-
Preact supports almost all the modern browsers nicely, such as- Chrome, Firefox, Safari, Edge, and IE11+.
Due to its plethora of plugins and add-ons, companies are taking a quick shift to it. It aims to deliver a few key goals, as given below-
While discussing ‘what makes Preact different from React’, the very first and most obvious difference is its size. When React weight 45kb (around), Preact is just 3kb.
However, there are some other reasons that add up to the differences. Though, many of them are either frivolous or can be removed completely by using the compatibility layer ‘preact-compat’.
Let’s see what are differences in this so-called replica of React-
Preact CLI is the Command-Line Interface that runs in the terminal of your system and helps you get started with building modern preact applications instantly, without any configuration. It is built upon the open-source tools such as Webpack, Terser, Babel, etc.
In order to get started with Preact CLI, you don’t need any prior knowledge and this, the simplicity, makes it the most preferred way to use Preact above all. You’ll get everything, right from hot reloading to critical CSS inlining, organized here for you. You can expect excellent and amazing performance from Preact projects with it.
npm i -g preact-cli
Let’s move further and bring the out-of-box features of it in the limelight-
Preact is an exquisite choice to develop progressive and interactive web applications. The small size of Preact is preferable when you’re on a tight loading performance budget. The large bundles of JS lead to stretch the loading times, but by shorting down the library code by Preact, it loads quicker.
Switching to Preact from React can shave off multiple KBs in your project and makes it faster by enhancing the interactivity in one RTT.
There are basically two approaches to switch from React to Preact, that is-
In your existing React app, you can try out Preact using the preact-compat compatibility layer. It lets you continue with your React/React DOM code with slight or no changes at all in the codebase.
This preact-compat package increases the bundle size by 2kb only and provides a major advantage by supporting the vast ecosystem of React modules. It enables you to work with Preact just like react and react-dom.
The process includes a few steps-
npm i -S preact preact-compat
{
"resolve": {
"alias": {
"react": "preact-compat",
"react-dom": "preact-compat"
}
}
}
Let’s make a simple ‘Hello World’ app. Our end goal here is to have an app that renders a text ‘Hello World’-
#With JSX
const App = <h1>Hello World!</h1>;
#Without JSX
const App = h('h1', null, 'Hello World');
#Injecting the app into DOM
render(App, document.body);
Congrats, we’re done here. You’ve built your first Preact app.
Recently, Preact Team took a huge step by releasing the newest version, i.e., Preact X, in the series after Preact 8.x. In a nutshell, it’s a tiny, complete and feature-rich package.
According to the team-
“We’ve rethought every bit and byte of our code and added a plethora of major features in the process. The same goes for compatibility enhancements with the React ecosystem to support more third-party libraries.”
It brings a bunch of new and exciting features, some of them are mentioned here-
There are so many other fixes in Preact 8.x, if you’ve filed to work with a library then give it a try with Preact X, I’m sure you won’t be disappointed.
Well, folks, I’ve tried to cover almost all the important matters (in-short) in this blog. Still, there’s so much more to know and learn about Preact. Whatever we learn can never be enough.
“Knowledge is infinite and indestructible. It can never be destroyed and remains in this universe in one or other forms.”
Are you confused about what to use, React or Preact?? Well, it completely depends upon your project requirements. Both of them have their own set of features and functionalities, though quite similar, but unlike enough.
You can use React for large scale apps, complex projects or other heavy stuff, whereas, the opposite is true in the case of Preact. It would work better for smaller and lighter apps. There is no point in building a complex application with Preact and importing several modules from the React ecosystem, wouldn’t it be better to just finish it with React??
Thanks for reading till the very end!! If you liked it then go further and reach out to our website https://www.habilelabs.io/blog/ for more similar stuff.