Solid JS Tutorial #9 - Fetching Data
Key Takeaways
This video tutorial demonstrates how to fetch data from a local JSON file using the Json Server package and integrate it into a Solid JS component template. The tutorial covers installing Json Server, running it to watch a JSON file, and using the createResource function in Solid JS to fetch the data.
Full Transcript
all right then gang so in this lesson what I'd like to do is show you how we can fetch data from somewhere and then inject that data into our component templates so what I want to do is fetch a list of products and then cycle through those products and output a card for each product where inside the card we can dynamically output a title maybe a description could be a price and a button to view more about that product so you could get this data from any kind of third-part API or your own API entirely up to you what we're going to do is get it from a local Json file now I've already created that I've created a data folder over here and inside that a db.json file inside that Json file we have a product property and the value of that is an array and inside the array I've got loads of different product objects so each product object has an ID property which is unique a title a description price and an image source now the image source is just basically a link to the images from my real merch store now if you want to grab all of this Json you can do it's on the GitHub repo over here who so you can just go to lesson nine then go into Data then db.json and just copy it all from here and paste it into this local file so the way we're going to use this is by using a package called Json server and what Json server does is it allows us to watch a Json file locally and wrap that with API endpoints so we can treat it a bit like an API and fetch that then from our components so you need Json server installed on your computer to do this so open up a terminal and you want to say npm install hyphen G for globally and then Json hyphen server and press enter once that's installed we can run it by saying Json server and then hyphen W to watch a file and then hyphen P to specify the port number it needs to be four thousand now the reason I'm doing this is because the default port number is 3000 and when we run npm run Dev inside the solid project that actually uses Port 3000 so I'm specifying 4000 right here then we need a path to the file we want to watch so that's dot forward slash into the data folder thendb.json when we do that it's going to give us an end point for the different resources that we have and the resources by the way adjust the property names so products in this case so we could click on this and that's now going to show us all of the products in the browser so this is all the Json data cool so we know that's working and now we can use this endpoint inside our home component and fetch that data so to fetch data inside a solid component solid actually gives us a function called create resource so the way this works is as follows we say const and then in square brackets some kind of value you can call this data if you want I'm going to call it products but ultimately this will be the data we get back when we make the Fetch and we set it equal to create resource like so now we need to import that as well at the top so let's do that so I will say import create resource and that comes from solid.js like so okay so we have this and what we need to do is pass a function into this function right here and it will use that function then to go out and fetch the data so we could do that inline but we could also create the function up here as well which I'm going to do so const and then I'm going to call it fetch product but again call it whatever you want it's going to be an asynchronous function and then inside this function we're going to use basically the fetch API to fetch the data so we'll say const response is equal to a weight and then Fetch and then it's going to be that end point that we had down here so I'm going to control C that and then paste it in right here so now we're going to fetch the data from right here we want to then return something we're going to return response dot Json like so so it's going to resolve this for us we need to pass the function in here fetch products so when it sees this function it's going to run this for us automatically it's going to fetch the data we're returning the Json right here it's going to resolve that and then we're going to get the product data or the products data right here now what I'd like to do is show you that this has worked now I cannot console.log products like this because remember functions only run once in solid.js and if we do this because this takes some time the function has already run we're not going to have any products by the time this console log runs and I can show you that if I save it and come over here we're going to open up the console I'm going to go to the home page and we can see products is undefined at the minute so what I'm going to do instead because we've not looked at client effects yet is I'm going to come down to the bottom of the template outside of this card and I'm just going to create another paragraph tag and I'm going to Output some Dynamic content here and all I'm going to do is run some JavaScript so console.log right here products like so and then also on this right here we get a loading property and that will either be true or false so I'm going to log that as well so we'll say after products products dot loading as well so that's going to be true to begin with then false once we actually have the products now this is going to be undefined to begin with but once it changes it's going to update because it's in the template right here we're going to see the update in the console and we should see a list of products so let me save that now and you can see to begin with we get undefined and true because it's loading then we get the data which is a list of product objects awesome and false because it's no longer loading because we have those products now so now what I'd like to do is instead of outputting all of this stuff right here I'd like to cycle through the products and output a better template for each product but I'm going to show you how we do that in the next lesson
Original Description
🚀🥷🏼Get early access to this entire course now on Net Ninja Pro:
https://netninja.dev/p/solid-js-tutorial
📂🥷🏼 Access the course files on GitHub:
https://github.com/iamshaunjp/solid-js-tutorial
💻🥷🏼 Modern JavaScript Tutorial:
On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja
On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
💻🥷🏼 Tailwind CSS Tutorial:
On Net Ninja Pro - https://netninja.dev/p/tailwind-css-tutorial
On YouTube - https://www.youtube.com/watch?v=bxmDnn7lrnk&list=PL4cUxeGkcC9gpXORlEHjc5bgnIi5HEGhw
🔗🥷🏼 Solid Playground - https://playground.solidjs.com/
🔗🥷🏼 Solid JS docs (getting started) - https://www.solidjs.com/guides/getting-started
🔗🥷🏼 Solid Router docs - https://github.com/solidjs/solid-router#getting-started
🔗🥷🏼 VS Code - https://code.visualstudio.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
Creta Frontend Interview Experience | Remote Role
Medium · JavaScript
The Case of the Ghost Tooltip
Medium · JavaScript
How I made a scroll-scrubbed video portfolio fast (Next.js 15 + GSAP + canvas)
Dev.to · Pratham Sharma
5 Reasons HTML Is About to Change Frontend Development
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI