Coding with Jesse

About Me

My name is Jesse Skinner. I'm a web consultant, developer, author, speaker, husband and father. I'm a Canadian from Barrie, Ontario, an hour drive north of Toronto.

I was born in 1980, and I've had a computer since 1983. As a child, I read programming magazines and typed the BASIC code into my Coleco Adam. I started running a BBS in 1992, chatting, sending messages, making ANSI graphics, trading files and playing games with people over the computer. I started using the Internet in 1994, and made a web page for my high school band in 1995.

I've been working as a professional web developer since 2001, and graduated from the University of Waterloo in 2004 with a "Bachelor of Mathematics" in Computer Science. I've been doing freelance web development since 2006, helping a wide variety of clients build web applications with JavaScript, CSS and HTML. Previously I heavily used jQuery, then React, and lately I've been mostly using Svelte.

I love to help people learn web development, and I enjoy sharing my learning and development process. I stream regularly on Twitch. I also record coding videos for YouTube.

I write regularly here on Coding with Jesse. I wrote a book for O'Reilly called Unobtrusive Ajax. I've also given a number of talks, and sometimes I teach at Georgian College.

Well, that's enough about me. What about you? Please feel free to get in touch.

React Hooks vs. Svelte

I get asked a lot on my Twitch channel to show a comparison of React and Svelte. I thought I'd record a short video to show everyone how to take a basic React example and rewrite it using Svelte:

Let's look at a basic example from the React Hooks documentation. Here we have a simple component with a button, and some text showing you how many times you've clicked the button. We're using some state in our component, to keep track of how many times the button was clicked. It's a nice, simple example of using a stateful component:

import React, { useState } from 'react';

function Example() {
    // Declare a new state variable, which we'll call "count"
    const [count, setCount] = useState(0);

    return (
        <div>
            <p>You clicked {count} times</p>
            <button onClick={() => setCount(count + 1)}>Click me</button>
        </div>
    );
}

What would this look like written with Svelte? Turns out, most of the code above is React boilerplate that we can do without. Let's start by commenting out all the boilerplate:

// import React, { useState } from 'react';

// function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);

// return (
<div>
    <p>You clicked {count} times</p>
    <button onClick={() => setCount(count + 1)}>Click me</button>
</div>;
// );
// }

That gets us some of the way, but that's still not valid Svelte. Svelte is an extension of HTML, so we need to put our JavaScript code in a <script> block, and change it to use a local state variable instead of React's useState function:

<script>
    // Declare a new state variable, which we'll call "count"
    let count = 0;
</script>

<div>
    <p>You clicked {count} times</p>
    <button onClick={() => setCount(count + 1)}>Click me</button>
</div>

This is very close to Svelte, but we have to change one more thing. We need to change React's onClick attribute to Svelte's on:click, and then change the click handler so it simply increments count:

<script>
    // Declare a new state variable, which we'll call "count"
    let count = 0;
</script>

<div>
    <p>You clicked {count} times</p>
    <button on:click={() => count++}>Click me</button>
</div>

All done! When you change React code into Svelte code, you spend most of your time deleting code, and deleting code feels amazing!

The major difference here is that your state is kept in local JavaScript variables instead of being tied up inside useState. This means you can set your state variable to a new value without calling a function, and that makes it possible to keep your component code very clean and succinct.

In fact, if you came up to me and said you had a new framework that was even simpler than Svelte, I'd have a hard time believing it! I mean, what could we remove from that Svelte component? Even Vanilla JavaScript would be way more complicated than this basic Svelte example. Svelte makes our web components as simple as possible, but no simpler.

Published on November 13rd, 2019. © Jesse Skinner