stateDiagram-v2
direction LR
[*] --> Count
Count --> Count : increment
Week 0 - Introduction
Welcome to the init to prod workshop.
Today we’ll discuss the motivation and what we hope to accomplish over the ten weeks.
Why use svelte? Let’s compare a simple interactive page with a button that increments a counter.
Below are links to the development environments Svelte Playground and Codepen
Svelte playground is great to experiment with Svelte, but stackblitz is very good for developing with any framework!
Introduction
Here is a link to a codepen, with some HTML elements and a button that can manipulate the a H1 heading.
There is a different version of this with a function declared using the arrow function syntax here
The three contrasting examples
You can view the completed svelte example live here: Counter Example - Svelte
<script>
let count = $state(0);
function increment() {
count++;
}
</script>
<button onclick={increment}>
Clicked {count}
{count === 1 ? 'time' : 'times'}
</button>You can view this vanilla example in codepen here
<html>
<head>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/6/w3.css">
</head>
<body>
<div class="w3-container w3-padding">
<button id="mybutton" onclick="increment()">This button has been clicked 0 times!</button>
</div>
<script>
let mybutton = document.getElementById("mybutton");
let count = 0
const increment = () => {
count++;
mybutton.innerText = `This button has been clicked ${count} ${count==1 ? "time" : "times"}`;
}
</script>
</body>
</html>You can view this react example live on codepen here Counter Example - React
import React from "react";
import { useState } from "react";
import ReactDOM from "react-dom/client";
function App() {
const [count, setCount] = useState(0);
const increment = () => {
setCount((x) => x + 1);
};
return (
<main>
<header>
<button onClick={increment}>
This button has been clicked {count} {count === 1 ? "time" : "times"}!
</button>
</header>
</main>
);
}
const root = ReactDOM.createRoot(document.getElementById("app"));
root.render(<App />);Webinar Recording
If you have an Oxford Brookes account, you can watch the webinar recording here: