Skip to content

Commit bc24479

Browse files
committed
Merge branch 'v2-svelte-course'
1 parent 90d53bb commit bc24479

33 files changed

Lines changed: 1167 additions & 40 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
authors:
3+
- alex-patterson
4+
cloudinary_convert: false
5+
cover: 'https://media.codingcat.dev/image/upload/v1683221180/main-codingcatdev-photo/courses/svelte/svelte-course'
6+
excerpt: 'Master Svelte: The Fast, Simple, and Powerful Framework'
7+
published: draft
8+
slug: svelte
9+
start: April 17, 2023
10+
title: 'Learn Svelte: The Ultimate Guide'
11+
youtube: https://youtu.be/OeEOWDTZV4k
12+
---
13+
14+
Svelte is a modern JavaScript framework that is quickly gaining popularity among web developers. It is known for its small bundle size, high performance, and simple syntax.
15+
16+
Svelte is a compiler, not a library. This means that it compiles your code to native JavaScript, which results in smaller bundle sizes and better performance. Svelte also uses a virtual DOM, which means that it only updates the parts of the DOM that need to be updated, which further improves performance.
17+
18+
Svelte's syntax is very simple and easy to learn. It is similar to HTML, with a few extensions for things like state and events. Svelte also has a number of built-in components, such as buttons, inputs, and lists.
19+
20+
If you are looking for a modern JavaScript framework that is small, fast, and easy to learn, then Svelte is a great option.
21+
22+
Here are some of the benefits of using Svelte:
23+
24+
- **Small bundle size:** Svelte's small bundle size means that your web pages will load faster. This is important for both users and search engines.
25+
- **High performance:** Svelte uses a virtual DOM, which means that it only updates the parts of the DOM that need to be updated. This results in high performance, even on large and complex web pages.
26+
- **Simple syntax:** Svelte's syntax is very simple and easy to learn. This makes it a great choice for beginners and experienced developers alike.
27+
- **Built-in components:** Svelte comes with a number of built-in components, such as buttons, inputs, and lists. This makes it easy to get started building web pages.
28+
- **Active community:** Svelte has a large and active community of developers. This means that there are plenty of resources available to help you learn and use Svelte.
29+
30+
If you are looking for a modern JavaScript framework that is small, fast, and easy to learn, then Svelte is a great optio
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
authors:
3+
- alex-patterson
4+
cloudinary_convert: false
5+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/what-is-svelte.png'
6+
excerpt: 'Svelte is a modern JavaScript framework that is designed to be fast, simple, and scalable.'
7+
published: draft
8+
section: Svelte
9+
slug: what-is-svelte
10+
start: April 17, 2023
11+
title: 'Learn Svelte: The Ultimate Guide - What is Svelte?'
12+
weight: 2
13+
youtube: 'https://youtu.be/sp7qvtvCFv8'
14+
---
15+
16+
## Svelte Summary
17+
18+
Svelte is a modern JavaScript framework that is quickly gaining popularity among web developers. It is known for its small size, high performance, and easy-to-learn syntax. Svelte is also very flexible and can be used to build a wide variety of web applications.
19+
20+
## Svelte Features
21+
22+
- Small size: Svelte is a very small framework, with a download size of just 4.1 KB. This makes it ideal for use on mobile devices and other low-powered devices.
23+
- High performance: Svelte is very performant, thanks to its use of the compiler to generate efficient JavaScript code. This means that Svelte applications can be loaded and run quickly, even on slow connections.
24+
- Easy-to-learn syntax: Svelte has a very simple and easy-to-learn syntax. This makes it a great choice for beginners, as well as experienced developers who want to build web applications quickly and easily.
25+
- Flexible: Svelte is a very flexible framework that can be used to build a wide variety of web applications. It is suitable for both small and large projects, and can be used to build both simple and complex user interfaces.
26+
27+
If you are looking for a modern, performant, and easy-to-learn JavaScript framework, then Svelte is a great option. It is quickly becoming one of the most popular frameworks in the world, and for good reason.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
authors:
3+
- alex-patterson
4+
cloudinary_convert: false
5+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/how-to-call-an-api.png'
6+
excerpt: 'Using fetch within Svelte provides a simple and consistent way to make HTTP requests.'
7+
published: draft
8+
slug: how-to-call-an-api
9+
start: April 17, 2023
10+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-course/tree/10-how-to-call-an-api?embed=1&file=apps/svelte-site/src/routes/%2Bpage.svelte'
11+
title: 'Learn Svelte: The Ultimate Guide - How to call an API'
12+
weight: 3
13+
youtube: https://youtu.be/O-72Vwpe5Ag
14+
---
15+
16+
## Fetching Data from a Web API
17+
18+
To fetch data from a web API, we can use the fetch API. The fetch API is a modern JavaScript API that makes it easy to make HTTP requests.
19+
20+
To use the fetch API, we first need to create a request object. The request object specifies the URL of the API and the HTTP method that we want to use.
21+
22+
Once we have created a request object, we can use the fetch() method to make the request. The fetch() method returns a promise that resolves to the response object.
23+
24+
The response object contains the status code, headers, and body of the response. We can use the status code to determine whether the request was successful or not.
25+
26+
If the request was successful, we can use the headers and body of the response to get the data that we need.
27+
28+
Below is an example of us getting using the Fetch API to get kitten data.
29+
30+
```svelte
31+
let kittens: { src: string; name: string }[] = [];
32+
33+
const getKittens = async () => {
34+
const res = await fetch('https://api.thecatapi.com/v1/images/search?limit=10');
35+
const kittenJson = await res.json();
36+
kittens = kittenJson.map((k: any) => {
37+
return { src: k.url, name: k.id };
38+
});
39+
};
40+
```
41+
42+
## Handling Errors
43+
44+
When we make a request to a web API, there is always the possibility that the request will fail. For example, the API may be down or the request may be invalid.
45+
46+
To handle errors, we can use the catch() method on the promise returned by the fetch() method. The catch() method takes a function as an argument. The function will be called if the request fails.
47+
48+
The function passed to the catch() method will receive the error object as an argument. The error object contains information about the error, such as the status code and the message.
49+
50+
## Caching Data
51+
52+
To improve the performance of our application, we can cache the data that we fetch from the web API. Caching the data means that we store the data in memory so that we don't have to fetch it from the API every time.
53+
54+
To cache data, we can use the cache() method on the response object. The cache() method takes a boolean value as an argument. The boolean value specifies whether we want to cache the data in memory.
55+
56+
If we set the boolean value to true, the response object will be cached in memory. The next time we make a request to the same API, the response object will be returned from the cache instead of being fetched from the API.
57+
58+
## Conclusion
59+
60+
In this blog post, we showed you how to use the fetch API to fetch data from a web API in Svelte. We also showed you how to handle errors and cache the data.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
authors:
3+
- alex-patterson
4+
cloudinary_convert: false
5+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/await-block.png'
6+
excerpt: 'Svelte makes dealing with promises easy by using an await block.'
7+
published: draft
8+
slug: await-block
9+
start: April 17, 2023
10+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-course/tree/11-await-block?embed=1&file=apps/svelte-site/src/routes/%2Bpage.svelte'
11+
title: 'Learn Svelte: The Ultimate Guide - Await Block'
12+
weight: 3
13+
youtube: https://youtu.be/kWZLl2cHkro
14+
---
15+
16+
## Svelte Await Blocks
17+
18+
Svelte await blocks are a powerful feature that allow you to write asynchronous code in a concise and easy-to-read way. They are similar to promises, but they have some key advantages.
19+
20+
First, await blocks are much easier to read. They are written in a natural, declarative style, which makes it easy to understand what the code is doing.
21+
22+
Second, await blocks are more efficient. They do not require the use of callbacks, which can make code more complex and less performant.
23+
24+
Third, await blocks are more flexible. They can be used with any asynchronous function, not just those that return promises.
25+
26+
## How to Use Svelte Await Blocks
27+
28+
To use Svelte await blocks, you first need to define an asynchronous function. An asynchronous function is a function that returns a promise.
29+
30+
Once you have defined an asynchronous function, you can use it in an await block. To do this, simply surround the call to the asynchronous function with the `await` keyword.
31+
32+
For example, the following code defines an asynchronous function that fetches a random number from a server:
33+
34+
```svelte
35+
async function getRandomNumber() {
36+
const res = await fetch(`/tutorial/random-number`);
37+
const text = await res.text();
38+
return text;
39+
}
40+
```
41+
42+
The following code uses the `getRandomNumber()` function in an await block:
43+
44+
```svelte
45+
{#await getRandomNumber()}
46+
<p>...waiting</p>
47+
{:then number}
48+
<p>The number is {number}</p>
49+
{:catch error}
50+
<p style="color: red">{error.message}</p>
51+
{/await}
52+
```
53+
54+
This code will first fetch a random number from the server. If the request is successful, the number will be displayed on the screen. If the request fails, an error message will be displayed.
55+
56+
## Advantages of Svelte Await Blocks
57+
58+
Svelte await blocks have several advantages over other ways of writing asynchronous code.
59+
60+
First, await blocks are much easier to read. They are written in a natural, declarative style, which makes it easy to understand what the code is doing.
61+
62+
Second, await blocks are more efficient. They do not require the use of callbacks, which can make code more complex and less performant.
63+
64+
Third, await blocks are more flexible. They can be used with any asynchronous function, not just those that return promises.
65+
66+
## Conclusion
67+
68+
Svelte await blocks are a powerful feature that allow you to write asynchronous code in a concise and easy-to-read way. They are a great way to improve the readability, efficiency, and flexibility of your code.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
authors:
3+
- alex-patterson
4+
cloudinary_convert: false
5+
cover: 'https://media.codingcat.dev/image/upload/v1682101455/main-codingcatdev-photo/courses/svelte/transitions-and-animations.png'
6+
excerpt: 'Make more appealing user interfaces by transitioning and animating DOM elements.'
7+
published: draft
8+
slug: transitions-and-animations
9+
start: April 17, 2023
10+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-course/tree/12-transitions-and-animations?embed=1&file=apps/svelte-site/src/routes/%2Bpage.svelte'
11+
title: 'Learn Svelte: The Ultimate Guide - Transitions and Animations'
12+
weight: 3
13+
youtube: https://youtu.be/vsB33e4ND3k
14+
---
15+
16+
Svelte is a modern and powerful front-end framework that allows developers to create dynamic and reactive user interfaces with ease. One of the key features of Svelte is its ability to create transitions and animations that enhance the user experience.
17+
18+
In this blog post, we will explore how to use Svelte transitions and animations with code examples.
19+
20+
## Transitions in Svelte
21+
22+
Transitions in Svelte are used to create smooth animations when an element is added, removed, or updated. Svelte provides several built-in transition functions that can be used out of the box, such as `fade`, `slide`, `fly`, and `scale`.
23+
24+
Let's take a look at an example of using the `fade` transition:
25+
26+
```svelte
27+
<script>
28+
import { fade } from 'svelte/transition';
29+
30+
let show = true;
31+
32+
function toggle() {
33+
show = !show;
34+
}
35+
</script>
36+
37+
<button on:click={toggle}>Toggle</button>
38+
39+
{#if show}
40+
<div transition:fade>Content that fades in and out</div>
41+
{/if}
42+
```
43+
44+
In the above code, we import the `fade` transition from the `svelte/transition` module. We also create a boolean variable `show` and a function `toggle` that toggles the value of `show` when the button is clicked.
45+
46+
When `show` is true, the content inside the `if` block is rendered with the `fade` transition. When `show` is false, the content is removed with the `fade` transition.
47+
48+
### Animations in Svelte
49+
50+
Animations in Svelte are used to create more complex and dynamic animations. Svelte provides several built-in animation functions that can be used out of the box, such as `spring`, `tweened`, and `crossfade`.
51+
52+
Let's take a look at an example of using the `spring` animation:
53+
54+
```svelte
55+
<script>
56+
import { spring } from 'svelte/motion';
57+
58+
let x = 0;
59+
60+
function move() {
61+
x = x === 0 ? 100 : 0;
62+
}
63+
</script>
64+
65+
<button on:click={move}>Move</button>
66+
67+
<div style="position: absolute; left: {x}px;" animate:{{ left: spring(x) }}>
68+
Content that moves left and right
69+
</div>
70+
```
71+
72+
In the above code, we import the `spring` animation from the `svelte/motion` module. We also create a variable `x` and a function `move` that toggles the value of `x` between `0` and `100` when the button is clicked.
73+
74+
The `div` element has a `style` attribute that positions it absolutely and sets its `left` property to the current value of `x`.
75+
76+
The `animate` directive on the `div` element uses the `spring` animation to animate the `left` property of the `div` element to the new value of `x` when it changes.
77+
78+
### Conclusion
79+
80+
Svelte transitions and animations provide powerful tools for creating dynamic and engaging user interfaces. With the built-in transition and animation functions, it's easy to add smooth and fluid animations to your Svelte components.
81+
82+
In this blog post, we've seen some examples of how to use transitions and animations in Svelte. However, there are many more functions and options available in the Svelte documentation, so be sure to check it out to learn more.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
authors:
3+
- alex-patterson
4+
cloudinary_convert: false
5+
cover: 'https://media.codingcat.dev/image/upload/v1682107607/main-codingcatdev-photo/courses/svelte/component-lifecycle.png'
6+
excerpt: 'Overview of the Svelte component lifecycle and its different phases, including the creation, update, and destruction phases.'
7+
published: draft
8+
slug: component-lifecycle
9+
start: April 17, 2023
10+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-course/tree/13-component-lifecycle?embed=1&file=apps/svelte-site/src/routes/%2Bpage.svelte'
11+
title: 'Learn Svelte: The Ultimate Guide - Component Lifecycle'
12+
weight: 3
13+
youtube: https://youtu.be/272Q-4eqSOM
14+
---
15+
16+
Svelte is a popular JavaScript framework for building web applications. It is known for its reactive approach, small bundle size, and excellent performance. One of the essential concepts in Svelte is the lifecycle of a component. A Svelte component lifecycle refers to the sequence of events that occur during the creation, update, and destruction of a component.
17+
18+
In this blog, we will explore the Svelte component lifecycle and its different phases.
19+
20+
## 1. Creation Phase
21+
22+
The creation phase is the first phase of the Svelte component lifecycle. It occurs when a component is instantiated for the first time. During this phase, the component initializes its state, sets up its event handlers, and creates the DOM elements it needs.
23+
24+
The following lifecycle hooks are available during the creation phase:
25+
26+
`oncreate()`
27+
28+
This hook is called once after the component is created and the DOM elements are added to the page. It is the ideal place to perform any initialization tasks that require access to the DOM, such as setting up third-party libraries or adding event listeners.
29+
30+
`beforeUpdate()`
31+
32+
This hook is called before the component is updated with new data. It is an ideal place to perform any preparation work before the component's state changes.
33+
34+
## 2. Update Phase
35+
36+
The update phase is the second phase of the Svelte component lifecycle. It occurs when a component's state changes, and Svelte updates the DOM accordingly.
37+
38+
The following lifecycle hooks are available during the update phase:
39+
40+
`onupdate()`
41+
42+
This hook is called after the component's state changes, and the DOM is updated. It is an ideal place to perform any post-update tasks that require access to the DOM.
43+
44+
`afterUpdate()`
45+
46+
This hook is called after the `onupdate()` hook and is an ideal place to perform any post-update tasks that do not require access to the DOM.
47+
48+
## 3. Destruction Phase
49+
50+
The destruction phase is the final phase of the Svelte component lifecycle. It occurs when a component is destroyed, either by removing it from the page or unmounting it.
51+
52+
The following lifecycle hook is available during the destruction phase:
53+
54+
`ondestroy()`
55+
56+
This hook is called once, just before the component is destroyed. It is an ideal place to perform any cleanup tasks, such as removing event listeners, clearing timeouts or intervals, or disposing of any resources.
57+
58+
## Conclusion
59+
60+
Understanding the Svelte component lifecycle is essential for building robust and performant applications. By using the lifecycle hooks, you can perform initialization, preparation, post-update, and cleanup tasks at the right time in the component's lifecycle. This can help you build applications that are easier to maintain, debug, and optimize.

0 commit comments

Comments
 (0)