My top favorite tools and libraries for Preact, React’s lightweight twin.
Preact is an alternative to the popular React library. It has fewer complexities and performs faster in comparison to React due to its smaller size of 3kb. Preact also uses the ES6 API similar to React.
It was developed by Jason Miller to provide a feature set similar to React in a smaller package. Preact has the fastest virtual DOM libraries and diff implementation. Some of its salient features are:
- Increased performance
- Decreased size
- Efficient memory usage
- Compatibility with the React API
- Extensive browser support including IE7 and IE8 by using polyfills
- Simpler code which is easier to understand
Uber, Lyft, and Housing.com are just a few of the organizations that have migrated to Preact and benefited both in terms of increased performance and reduced size. The case study of Uber is well known. Uber migrated from its native app to a web client that provides an app-like experience via web browsers. The move to Preact ensured that browsers loaded faster, resulting in an increased number of ride requests.
Preact has many tools and libraries to support and extend its functionality. Here are my top 10 picks.
1. Redux Zero
Redux Zero is a lightweight state container based on Redux . Written in TypeScript, it supports not only Preact, but other libraries and frameworks like React, Svelte, and Vue. It is smaller in size and more efficient as it has a single store and no reducers.
Use the NPM command given below to install Redux Zero:
Coding with Redux Zero is simple and straightforward. Follow these steps:
- Create the store.js file as follows to create a store that will be used to manage application state.
2. Actions are used to change the state of the store. Create the Action.js file as follows:
3. As actions are pure functions, they are bound to the store.
4. Now let’s connect all of these components in the index.js file.
Refer to the working code of this example here .
2. Bit.dev (for Preact and React)
Bit versions each component individually and when you’re ready to share it, it builds and tests it in an isolated environment to make sure you’re sharing truly reusable components that are not coupled to your project.
You and your team can browse through your collection using Bit’s search-tools and playground,
npm install a shared component just like any other package or
bit import it, to modify it in your local dev environment (and even push the modified version back to the shared collection).
It’s a great way to gradually build a modular component library. Whenever you build a reusable component that’s worth sharing — simply push it to the shared Bit collection.
Use it to improve collaboration with your team, maximize code reuse, build more scalable and maintainable code, and keep a consistent UI.
Bit supports Preact, React, React with TypeScript, Vue, Angular and many more.
3. fpreact: (Functional Preact)
fpreact is an alternative API used to create Preact components. The API provides state management similar to redux and facilitates functional programming by removing the use of this.
fpreact is based on elm, which is used to create graphical UIs for web browsers. fpreact uses a domain-specific programming language and is a competitor of React.js and Vue.js.
Below is the simple ES6 + JSX code for a ‘Hello World’ application:
It also works with the data layer as a props composition. These are some of the benefits of using Proppy:
- Makes components stateless, as Proppy accepts and renders the props
- Makes functional props as they are composed within functions and grow easily
- It sets an application-wide provider that is global and accessible from any component
- It is interoperable as it integrates easily with other libraries
- Makes testing and rendering easier through many of your favorite tools
Here is a simple example of using ProppyJS with Preact:
ClearX maintains the application state and is an alternative to Redux and MobX. It binds application state to UI components using an interface and provides getters and setters for deep properties of nested data.
React class components and Function UI components work well with ClearX. You can simply install ClearX with the following NPM command:
Follow these steps to use ClearX:
- Create a datastore:
ClearX first creates the stores and then uses the path to set deep properties of data. So they can be defined for non-existing or undefined properties.
The datastore can be of any type.
2. Bind data to the UI store as follows:
Now it can be bound to the class store or selected path of the store using the useState() hook.
ClearX also provides multiple API’s to operate and find the data as per applications.
Preact-urql uses urql with the Preact core and hooks. It is used to improve the usability and stability of customized GraphQL infrastructure. To start, you need to import a single package from the framework of your choice. But it also has collections of connected parts to extend the application.
When using GraphQL, a primary data layer is created by urql to handle content-heavy pages through “Document Caching” and Normalized caching for data-heavy apps. It creates a user interface using trees of components and elements.
This is an example of Preact-urql where the render() function accepts the tree description and creates structures. It then appends the structure to the DOM element of a parent as a second argument. Thereafter the render() function uses the existing tree and calculates the difference in the outputs of current and previous to update the DOM.
Apart from using Preact-CLI, here are some tools to make your coding with Preact much easier.
nwb is a zero-configuration development setup but also supports configuration and plugin modules. It provides a quick development environment for Preact and its components.
This is an example of Preact components to whip up a lightbulb.js:
Use the following command to install essential dependencies and start a webpack development server.
For a production build, use “nwb preact build”:
nwb provides automatic dependency installations, style preprocessing, rendering the entry module, and hot module replacement along with the development and testing environment.
Rewired allows you to tweak the create-react-app webpack config(s) without the need to eject or create a fork of the react-scripts. It allows you to add plugins, loaders, and all the benefits of the create-react-app command without the restrictions of “no-config.”
Use the following NPM command to install rewired:
To make changes to config files, you can create config-overrides.js within the root directory.
“Flip” the existing calls of Preact-scripts in npm to start, build, and test. All scripts other than those required to eject can be flipped.
You can now start and run the server, and set a custom path for config-pverride.js..
Preact-CLI-postcss will provide the postcss.config.js file and disable the default postcss config so that you can have better control. To install, use the following command:
It adds your properties to preact.config.js as follows:
10. Create Preact APP
Create Preact APP can be used to create Preact apps with no configurations. It is similar to create-react-app, but uses Preact. Some of its important characteristics are:
- One dependency: One build dependency, uses webpack, Babel, ESLint and along with a cohesive, curated experience on top.
- No Configuration Required: No requirement of configuration for production and development builds.
- No Lock-in: Anytime you can “eject” to a custom setup, it moves all configuration and builds dependencies with a single command.
You can use the below command to set and start the dev-server:
This command will create the below folder structure with pre-installed transitive dependencies:
│ ├── favicon.ico
│ ├── index.html
│ └── manifest.json
Use the following commands to run your application: