Solid JS Tutorial #16 - Derived Values

Net Ninja · Beginner ·🌐 Frontend Engineering ·3y ago

Key Takeaways

The video demonstrates how to use derived values in Solid JS, a JavaScript framework for building user interfaces, by creating computed values that depend on other signals or store values.

Full Transcript

all right then gang so in this lesson I want to talk about derived values which is another way of saying computed values and the arrived values are basically values that depend on other signals for example I've got a signal here which is storing a count and that's going to increase by one every time I click on this button now I'm also outputting the double of whatever the count currently is right here and this is what's known as a derived value because it's derived from some of the signal value it depends on it and the way we create these derived values is by wrapping them in a function and because we use the count signal inside this function solid knows to rerun this when the count changes to update the return value of this double function and therefore we see that update over here as the count updates so derived values are just functions that return a value that depend on the value of other signals and also you can have derived values which use store values as well and the principle is exactly the same we just make a function that returns is a value based on some other store property value so that's the basics of derived values now let's put this into action okay so back in our project what I'd like to do is show how many items are currently in the cart now if we think about it we're not actually storing that number explicitly anywhere because if we go to our context then it's just an array of products now each product has a quantitative property so we can add all those up which is what we're going to do but there's no actual value directly that we can access that says how many items are in it so this is going to have to be a derived value so let's go to the home page because that is where the header is isn't it no it's not sorry it's in the app page of the app component so this is the header right here and we have the cart right here so I want to show just here in parentheses how many products we have in the cart so to do that we're going to have to use the cart context because we need access to all the items inside it so let's do that we'll say const items is equal to use cart context hit enter to import that right here so now we have the items now we need to create the derived value which is a function so we say const and we'll call it quantity set that equal to a function and inside here we just return a value now that's just going to be one number now in order to work this out we have to cycle through each item in this array touch up how many we have in total using that quantity property and then just return a single number so what I'm going to do is paste something in right here and then explain it so we're returning a value and we're taking the items and we're using the reduce method audit so the reduce method basically just Cycles through an array looks at all of the different values in that array and we can reduce everything into one single value essentially now the value we're reducing to is going to be a number so it fires this function right here for each item in the array the second argument is the accumulator so this is the value we're reducing to and it starts off at zero so each time we fire this function we get access to the current value of the accumulator but also the current item in the items so it's going to start off as the first item then the second and the third and so forth now each time we find this function we turn a value and that return value becomes the accumulator for the next iteration does that make sense and then after all iterations after we've gone through every item in the array whatever this is is the value this returns so we take the current accumulator which starts off at zero and then the current product and look at the quantity of that and add them together so zero plus the quantity of the first product so that might be two so then this becomes two for the next iteration then we cycle on to the next product and we say this is two now and the current product so the quantity of the current product the second one could be four so we take two and we add four and it becomes six and so forth so by the end of it we're going to have a number and that's the amount of items inside the cart all right so hope that makes sense now we can use that down here so inside curly braces we can say Quantity and invoke him save that and come over to the browser and we can see we have five items in the car well we do two plus two plus one is five and if we go to another product and start adding them six seven eight let's try something else add to cart 9 10 Etc so this is working I'm also going to do another derived for you inside the cart page which is going to be the total price and that's based on all of these items right here so let's go back to the cart page to do this we already have access to the items right here so we just need to make that derived value which is a function I'm going to call Total and we set that equal to an arrow function inside here we need to return the total amount that this is going to cost so 2 times 12 plus 2 times 21 plus 21 Etc so what I'm going to do again is just paste something in and we're using the reduce function again on the items to cycle through them and this time we're taking the accumulator and we're adding this thing each time around so the product quantity times the product price so in this case it would be 2 times 12 and then we add that to 2 times 21 then we add all that to 1 times 21 Etc until we've done them all and then we have a figure for the total amount so now we can output that right down here at the bottom and I'm just going to copy this from my repo because there's lots of Tailwind classes in it so we have a paragraph tag with some margin padding border and font bold applied to it and we say the total cart price is the total right here and then a pound sign just before that so this grabs this value here and remember the whole ID of these derived values is that they update whenever the items change so it's always going to be a Fresh Value so let's say this and try it out and we can see the total cart Price Right Here 147 let's add something let's take a look at this it's 12 so it should become one five nine yep and it does awesome so that my friends is derived values

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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to create derived values in Solid JS, which are computed values that depend on other signals or store values, and how to use them in UI components to update the display dynamically.

Key Takeaways
  1. Create a signal to store a value
  2. Create a derived value by wrapping a function around the signal
  3. Use the derived value in a UI component
  4. Update the signal to see the derived value update dynamically
💡 Derived values in Solid JS are a powerful way to create reactive UI components that update dynamically based on changes to underlying data.

Related Reads

Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →