Vite / Vite and Preact
Setting Up a Preact/Vite Project
In this tutorial, you will learn how to set up a new project using Preact and Vite. We will walk through the steps of initializing a new Vite project and integrating Preact into i…
Section overview
5 resourcesExplores the integration of Vite with the Preact library
1. Introduction
In this tutorial, we will set up a Preact/Vite project. Preact is a fast, lightweight JavaScript library for building user interfaces, similar to React but with a smaller footprint. Vite is a build tool that provides a faster and leaner development experience for modern web projects. By the end of this tutorial, you will have a solid understanding of how to create a new project using these technologies.
You will learn:
- How to initialize a new Vite project
- How to integrate Preact into a Vite project
Prerequisites:
- Basic understanding of JavaScript
- Node.js and NPM installed on your computer
2. Step-by-Step Guide
2.1. Installing Vite
First, you need to install create-vite, a scaffolding tool for Vite projects, by running the following command in your terminal:
npm init @vitejs/app
2.2. Creating a Vite/Preact Project
After the installation, you will be prompted to provide a project name and select a template. Choose the preact template:
? Project name: my-preact-app
? Select a template: (Use arrow keys)
vanilla
vue
❯ preact
react
lit-element
Then, navigate into your new project folder:
cd my-preact-app
2.3. Installing Dependencies
Install the project dependencies by running:
npm install
2.4. Running the Project
You can start the development server by running:
npm run dev
Your Preact/Vite project should now be running at http://localhost:5000.
3. Code Examples
Your main application component is defined in src/App.js, here’s what it looks like:
import { h } from 'preact'
import { useState } from 'preact/hooks'
import logo from './logo.png'
export default function App() {
const [count, setCount] = useState(0)
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<button onClick={() => setCount((count) => count + 1)}>
Click me
</button>
<p>Count: {count}</p>
</header>
</div>
)
}
This main component displays a logo, a button that increments a counter, and the current count state.
4. Summary
In this tutorial, we have set up a Preact/Vite project. We learned how to initialize a new Vite project and integrate Preact into it.
Next steps could include learning more about Preact's features, such as its hooks API and component lifecycle methods. For further reading, check out the official Preact documentation and the official Vite documentation.
5. Practice Exercises
Exercise 1: Modify the App component to display a list of 5 items of your choice.
Solution:
export default function App() {
const items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'];
return (
<div className="App">
<header className="App-header">
<ul>
{items.map(item => <li>{item}</li>)}
</ul>
</header>
</div>
)
}
Exercise 2: Create a new component that accepts a prop and displays it. Use this component in the App component.
Solution:
function Greeting({ name }) {
return <p>Hello, {name}!</p>
}
export default function App() {
return (
<div className="App">
<header className="App-header">
<Greeting name="Preact" />
</header>
</div>
)
}
Keep practicing by creating more components and experimenting with Preact's features!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article