Tag: web development

  • You Don’t Need React: Build Your Own Minimal UI Library in Vanilla JavaScript

    Vanilla JavaScript vs React: Choosing the Right Tool for Web Development - Async Labs - Software Development & Digital Agency

    React has become the default choice for building user interfaces, but it’s not always necessary. For many projects, a simple, reactive UI can be built with just a few hundred lines of vanilla JavaScript—no build tools, no transpilers, no dependencies. This article explores how to create a minimal UI library from scratch, demystifying the core concepts behind React and showing that you can have component-based architecture, state management, and efficient DOM updates without the overhead.

    By understanding how frameworks work under the hood, you’ll become a better developer, whether you stick with React or go vanilla. Let’s dive into the world of custom UI libraries and see what it takes to build your own.

    Why Consider a Custom UI Library?

    React is powerful, but it comes with a cost: a large bundle size, a learning curve, and a complex build setup. For small projects, static sites, or simple widgets, this overhead can be overkill. A minimal UI library written in vanilla JavaScript offers several benefits:

    • No dependencies: You control every line of code, reducing security risks and maintenance burdens.
    • Fast loading: With no framework to download, your pages load faster, especially on slow connections.
    • Simpler debugging: You know exactly what your code does, making it easier to trace bugs.
    • Educational value: Building your own library teaches you the core concepts behind React, making you a more effective developer.

    Of course, this approach isn’t for everyone. React provides a mature ecosystem, developer tools, and team scalability. But for many use cases, a custom solution is not only viable but also elegant.

    Core Concepts: State, Rendering, and DOM Updates

    To build a reactive UI library, you need three things:

    1. State management: A way to store and update data that triggers UI updates.
    2. Rendering: A function that converts state into DOM elements.
    3. DOM updates: A mechanism to apply changes to the actual page efficiently.

    React uses a virtual DOM to minimize direct manipulation, but you can achieve similar results with simpler techniques.

    State Management with a Simple Store

    The simplest approach is to create a state object and a way to subscribe to changes. Here’s a minimal implementation using a Proxy:

    “`javascript
    function createStore(initialState) {
    let state = initialState;
    const listeners = new Set();

    const proxy = new Proxy(state, {
    set(target, property, value) {
    target[property] = value;
    listeners.forEach(listener => listener());
    return true;
    }
    });

    return {
    get state() { return proxy; },
    subscribe(listener) {
    listeners.add(listener);
    return () => listeners.delete(listener);
    }
    };
    }
    “`

    This store lets you read and modify state, and it notifies subscribers whenever a property changes. It’s a simple reactive system.

    Rendering Components as Functions

    In React, components are functions that return JSX. In vanilla JS, you can have functions that return DOM nodes. For example:

    javascript
    function Counter({ count, onIncrement }) {
    const div = document.createElement('div');
    const button = document.createElement('button');
    button.textContent = 'Increment';
    button.addEventListener('click', onIncrement);
    const span = document.createElement('span');
    span.textContent = `Count: ${count}`;
    div.append(span, button);
    return div;
    }

    This function takes props and returns a DOM element. It’s pure and easy to test.

    Efficient DOM Updates with Targeted Re-renders

    Instead of a virtual DOM, you can re-render only the parts that change. One approach is to use a simple diffing algorithm that compares old and new DOM trees. But for small apps, you can simply re-render the entire component tree on state changes, as long as you’re careful with event listeners.

    A more efficient method is to use a MutationObserver or to manually update specific nodes. For example, you can store references to DOM nodes and update their text content directly:

    javascript
    function updateCounter() {
    span.textContent = `Count: ${store.state.count}`;
    }

    This avoids re-creating the whole DOM, which is faster for small updates.

    Building a Minimal Library: Step-by-Step

    Let’s put it all together into a tiny library. We’ll create a createApp function that takes a root component and mounts it to the DOM.

    “`javascript
    function createApp(Component, root) {
    const store = createStore({});
    let currentVNode = null;

    function render() {
    const newVNode = Component(store.state);
    if (currentVNode) {
    // Simple diff: replace the root element
    root.replaceChild(newVNode, currentVNode);
    } else {
    root.appendChild(newVNode);
    }
    currentVNode = newVNode;
    }

    store.subscribe(render);
    render();

    return {
    setState: (updater) => {
    // Merge state changes
    Object.assign(store.state, updater);
    }
    };
    }
    “`

    This is a bare-bones version, but it demonstrates the core idea: a component function, a store, and a render loop.

    Handling Events and User Interaction

    In React, you attach event handlers via JSX. In vanilla, you use addEventListener. When you re-render, you need to avoid attaching duplicate listeners. One way is to use event delegation: attach a single listener to a parent and handle events based on event.target. This is efficient and avoids cleanup issues.

    For example:

    javascript
    root.addEventListener('click', (event) => {
    if (event.target.matches('button')) {
    // Handle button click
    }
    });

    This way, you don’t need to re-attach listeners on every render.

    Managing Lists and Keys

    React uses key props to efficiently update lists. In a minimal library, you can use a similar approach: give each list item a unique key and use it to identify which items to update or remove.

    A simple diffing algorithm for lists might look like:

    javascript
    function diffLists(oldList, newList, parent) {
    // Remove old items not in new list
    // Add new items
    // Update existing items by key
    }

    This is more complex, but for small lists, you can simply re-render the entire list container.

    Performance Considerations

    Direct DOM manipulation can be faster than a virtual DOM for small apps because you avoid the overhead of creating and diffing virtual trees. However, for large apps with frequent updates, React’s optimizations (like batching and memoization) become valuable.

    In your custom library, you can optimize by:

    • Using requestAnimationFrame to batch updates.
    • Avoiding unnecessary re-renders by comparing state changes.
    • Using document.createDocumentFragment for batch DOM insertions.

    When to Use Vanilla vs. React

    A custom library is great for:

    • Learning purposes.
    • Small widgets or embedded components.
    • Projects where you want zero dependencies.
    • Situations where you need fine-grained control over performance.

    React is better for:

    • Large, complex applications.
    • Teams that need consistent patterns and tooling.
    • Projects that benefit from React’s ecosystem (hooks, suspense, etc.).

    Conclusion

    Building a minimal UI library in vanilla JavaScript is not only possible but also a valuable exercise. It demystifies React’s internals and gives you a deeper appreciation for the work frameworks do. While it’s not a replacement for React in production, it’s a powerful tool for learning and for simple projects. So next time you reach for React, consider whether you really need it—or if a few hundred lines of vanilla JS will do the job.

    In the end, the choice between React and a custom vanilla solution depends on your project’s needs. For small, focused applications, a minimal library can be simpler, faster, and more transparent. For large-scale projects, React’s ecosystem and optimizations are hard to beat. But regardless of your choice, understanding how to build a UI library from scratch will make you a more versatile and knowledgeable developer.

    Summary

    • You can build a reactive UI library in vanilla JS with just a few hundred lines of code.
    • Core concepts include state management, component functions, and efficient DOM updates.
    • A simple store with a Proxy can handle reactivity, and components can be plain functions returning DOM nodes.
    • Event delegation and targeted updates can avoid performance pitfalls.
    • This approach is educational and suitable for small projects, but React remains better for complex apps.

    FAQ

    Q: Is this library production-ready?
    A: No, it’s a minimal educational example. It lacks features like keyed list diffing, lifecycle methods, and error boundaries that you’d need for a real app.

    Q: How does this compare to Preact or Hyperapp?
    A: Those are more mature and feature-complete. This is a from-scratch implementation to teach concepts, not a drop-in replacement.

    Q: Do I need to know advanced JavaScript to understand this?
    A: A basic understanding of functions, objects, and DOM manipulation is enough. The article uses modern features like Proxy, but they’re explained.

    Q: Can I use this in a real project?
    A: You could, but be prepared to handle edge cases yourself. It’s better suited for learning or for very simple widgets.

    Q: Why not just use React?
    A: For small projects, React’s overhead (bundle size, build setup) may not be worth it. A custom solution gives you full control and a smaller footprint.

  • Build a Website in 20 Minutes: A Step-by-Step Guide for Beginners

    How To Build A Website in 20 Minutes (WordPress Tutorial 2023)

    Have you ever thought about building a website but assumed it would take days or weeks of coding? In today’s digital age, that’s no longer true. With the right tools and a clear plan, you can create a functional website in just 20 minutes. This isn’t about building a complex web application with databases and user logins—it’s about getting a simple, good-looking site live on the internet quickly. Whether you’re a small business owner needing a landing page, a student wanting to showcase a project, or just curious about how websites work, this guide is for you.

    We’ll walk through a practical, hand-coded approach that requires no prior experience. You’ll learn how to structure a basic HTML page, style it with CSS, add a touch of interactivity with JavaScript, and deploy it for free using modern hosting services. By the end, you’ll have a live URL you can share with friends or clients. The best part? You’ll gain a foundational understanding of web development that you can build upon later.

    What You’ll Need

    Before we start, make sure you have a code editor (like Visual Studio Code, which is free) and a modern web browser (Chrome, Firefox, or Edge). You don’t need to install anything else—we’ll use online tools for hosting. If you don’t have a code editor yet, download Visual Studio Code from code.visualstudio.com; it takes about a minute.

    Step 1: Set Up Your Project Folder (1 minute)

    Create a new folder on your computer called my-website. Inside it, create two files: index.html and styles.css. You can do this in your code editor by going to File > New File and saving them with those names. This folder will hold all your website’s files.

    Step 2: Write the HTML Structure (5-7 minutes)

    Open index.html in your editor. We’ll build a simple landing page with a hero section, an about section, a contact form, and a footer. Here’s the code:

    “`html

     

    Welcome to My Website

    Your go-to place for awesome content.

    About Me

    I’m a passionate creator who loves building things for the web.

    Contact Me



    © 2025 My Website

     

    “`

    This is a semantic structure—each part has a clear purpose. The header introduces the site, section elements organize content, and footer holds copyright info. The link tag connects our CSS file, and the meta tags ensure proper rendering on mobile devices.

    Step 3: Style with CSS (5-7 minutes)

    Now open styles.css and add some styling to make it look professional. We’ll use a clean, modern design with a color scheme and responsive layout:

    “`css
    body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
    color: #333;
    }

    header {
    background-color: #4CAF50;
    color: white;
    text-align: center;
    padding: 50px 20px;
    }

    section {
    padding: 20px;
    margin: 20px auto;
    max-width: 600px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    h2 {
    color: #4CAF50;
    }

    input, textarea {
    width: 100%;
    padding: 10px;
    margin: 10px 0;
    border: 1px solid #ccc;
    border-radius: 4px;
    }

    button {
    background-color: #4CAF50;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    }

    button:hover {
    background-color: #45a049;
    }

    footer {
    text-align: center;
    padding: 20px;
    background-color: #333;
    color: white;
    }
    “`

    This CSS sets a consistent font, centers content, and adds visual appeal with colors and shadows. The max-width and margin: auto ensure the site looks good on both desktop and mobile—a basic form of responsive design.

    Step 4: Add Interactivity with JavaScript (3-5 minutes)

    To make the contact form functional (even if just showing an alert), create a new file called script.js and link it in your HTML just before the closing </body> tag:

    “`html

    “`

    In script.js, add:

    javascript
    document.querySelector('form').addEventListener('submit', function(event) {
    event.preventDefault();
    alert('Thank you for your message! I\'ll get back to you soon.');
    });

    This prevents the page from reloading and shows a friendly alert. It’s a simple example of how JavaScript can enhance user experience.

    Step 5: Test Locally (2 minutes)

    Open index.html in your browser by double-clicking the file. You should see your styled page. Try clicking the button—it should show the alert. If something looks off, check your code for typos. This local test ensures everything works before we go live.

    Step 6: Deploy for Free (2-3 minutes)

    Now for the magic part—getting your site online. We’ll use Netlify Drop, which allows you to drag-and-drop your folder and get a live URL in seconds. Go to app.netlify.com/drop, drag your my-website folder onto the page, and wait a few seconds. You’ll see a URL like random-name.netlify.app. That’s your live website! You can share it with anyone.

    Alternatively, if you prefer GitHub Pages, you’d need a GitHub account and a repository, which takes a bit longer but is also free. For this tutorial, Netlify Drop is the fastest.

    Understanding What You Built

    You’ve just created a static website—a site that shows the same content to every visitor. It’s perfect for portfolios, landing pages, or event pages. The HTML provides structure, CSS handles styling, and JavaScript adds interactivity. This is the foundation of all web development.

    Next Steps for Going Further

    Now that you have a live site, you might want to expand it. Consider adding more sections, a navigation menu, or even a blog. You can learn about responsive design to ensure it looks great on all devices, or explore CSS frameworks like Bootstrap to speed up styling. If you want dynamic content, you’ll need to learn about back-end development or use a content management system like WordPress.

    Remember, this 20-minute build is a starting point. Real-world websites require attention to accessibility, SEO, and performance. But you’ve taken the first step—and that’s what matters.

    Building a website in 20 minutes is not only possible but also a fun and rewarding experience. You’ve learned the core trio of web development—HTML, CSS, and JavaScript—and deployed a live site for free. This hands-on approach demystifies the process and gives you a solid foundation to build upon. So go ahead, share your new website, and keep experimenting. The web is your canvas!

    Summary

    • You can build a simple, static website in 20 minutes using HTML, CSS, and JavaScript.
    • The process involves setting up a project folder, writing code, testing locally, and deploying with free services like Netlify Drop.
    • This approach is perfect for beginners who want a quick online presence without complex tools.
    • The result is a functional landing page with a hero section, about, contact form, and footer.
    • Remember that this is a starting point; real-world sites need more attention to accessibility, SEO, and performance.

    FAQ

    Q: Do I need to know how to code to follow this tutorial?
    A: No, this tutorial is designed for absolute beginners. We explain each line of code in simple terms, and you can copy-paste the examples directly.

    Q: Can I use a website builder instead of hand-coding?
    A: Absolutely! Platforms like Wix or Squarespace are even faster and require no coding. However, hand-coding gives you more control and teaches you the fundamentals.

    Q: Is the website I build mobile-friendly?
    A: Yes, we included a viewport meta tag and used relative units and max-width, which helps the site adapt to different screen sizes. For more advanced responsiveness, you’d add media queries.

    Q: How can I get a custom domain like www.mywebsite.com?
    A: You can purchase a domain from services like Namecheap or Google Domains and then connect it to your Netlify site. Netlify provides instructions for this.

    Q: What if I want to add more pages to my site?
    A: You can create additional HTML files (e.g., about.html, contact.html) and link them with <a> tags. For a multi-page site, you’d also want a navigation menu.