Solid JS Tutorial #12 - Using Stores
Key Takeaways
This video tutorial covers the use of stores in Solid JS to manage state, including creating stores, accessing and updating store data, and using paths to update specific properties or array elements. The tutorial also compares the use of stores to signals and demonstrates how to use stores to update nested objects and arrays.
Full Transcript
or at the gang so in this lesson I want to talk about another way of managing the state using something called stores and I mentioned those briefly back when we talked about signals earlier on in the playlist and I said that although we can use signals to store objects and arrays they're not optimized as well as stores to deal with them so instead of using signals for deeply nested objects and arrays we can use stores instead which are a little bit more optimized for those types of data because stores treat each object property as its own signal or each property value as its own signal and it's the same with arrays it would treat each item in an array as its own signal essentially so when we're updating the values of these objects or arrays it lets us pinpoint exactly the one part of them that we need to update and we don't have to treat it so much as a single value as we would when we're using Create signal so let's have a look at some examples of stores now in this solid.js playground so I've already created a couple of stores here for us to play around with and you're going to see we've used this function create store to create them and we've imported that up here at the top as well and this is pretty much the only difference on the surface of things at the minute because before this was create signal but we still pass in the same object into this and we still get two values back in Array format we have person to access the store data and we have set person which is a Setter function to set the new value now we also have an example down here using an array as a value so create store again we pass in the array and each item in the array is just an object with a title a price and an ID and we call those products and the setup function is set products now the first difference is when we come to access the values because these are no longer essentially getter functions but kind of proxy objects and we don't need to invoke products in order to get it we don't need to invoke person in order to get it instead we just say person and then dot whatever value we want so dot name dot first.name.last down here we use a four component to cycle through the product again we don't invoke it and we output a bit of template for each product just a paragraph tag where we get the title and the price and we can see those here and the names right here the real difference now apart from non-invoking this is how we update things because like I said a minute ago solid.js when we're using stores treats each property value as its own signal so we can update those independently of the entire value so I'm going to show you how we can do that now so first of all I'm going to come down here and I'm going to place a button at the bottom of the template and this button is going to be used to maybe I don't know change one of the products update a product in fact we'll do one for the name as well so let's do a button up here like so and close this off and in this one we will say change the last name so we're going to click on this to update the last name of the person and then down here we'll say change a product okay so we'll do the name one first of all so I'm going to say on click and we set that equal to some kind of function now I'm going to call that function change name and we need to make it up here so let's do a function below this door for the person so function change name and then inside here we want to use this function oops we want to use this function set person to update the name so we say set person and previously when we use signals we basically pass through the entire object again so with copy all that where you'd pass it in here and we'd just change the first name or something now we don't have to do that when we're using stores instead we can pinpoint which value or which property we want to update so I could say for example first I want to access the name property which is this then as a second argument I would want to access the first property which is this and then once I've reached my destination the new value of that will be the third argument and that could be Mario for example so this right here is a path to whatever property we want to update okay let's spell function correctly so now when we click on this button down here change name is that working okay I've used Capital here change name so if I click on this it should update the first name to Mario we do say change the last name let's change this to First so change the first name click on that and it changes it to Mario so you can look at this as a path to whatever property you want to update okay all right so let's do another example with the products so down here I'm going to create another function so const or rather we'll just say function and this will be change product and what we'll do is we'll pass in the ID of the product that we want to change we're not going to use it originally right now but we will do in a minute so I'm going to say set products in here and again we have like a path to the product that we want to update this time it's an array so we can pass through the index first of all of the item we want to update so I could say zero to get the first item in the array and that's what I want to update then it would be the property so I could change the price property for example so I'd specify that as the second argument and then finally the value which could be 25. like so so now we're updating this 10 value to 25. I'm going to get rid of that ID because we don't need it just yet but down here I'm going to say on click and I'm going to set that equal to change product like so so let me come down here change the product and you can see this is now 25 pounds awesome so again when we're working with arrays this is like a path to the value that we want to update let me comment this out and do one more example because as well as a path right here to the array number the index we can also use a function and we can use that for any argument to specify or to work out what position or what property we want to update so let me do an example set products now the first argument is going to be a function right here which finds one of these items so it will fire a function for each of these items and do a check if we return true for that particular item then it will select the item in the array does that make sense and update that one so what I'm going to do is wrap this inside another inline function just so we can pass through in ID property so say for example I want to update the product with the ID of 2 which is this one right here so now we get the ID as an argument and we can use the ID in this function so like I said this function Fires for each product and we get access to that product as an argument which I'll call P and I want to return true where P dot ID is equal to the ID that we pass in so I'll say p dot ID is equal to the ID that we pass in and that means that it's going to return true for this one because P to ID 2 is going to equal 2. so it will select this object right here at this position in the array for the second argument again I'll just say I'll update the price and then the new value which is going to be 50. so I think that's pretty much it so now if I change a product down here it should be this one that changes to 50 which it does awesome so hopefully you can see how much easier it is to update different properties or different array elements and properties in those array elements using stores instead of signals and we're going to be using stores in our project in probably one or two lessons time but for now this should give you a general overview of how we create stores and how we update different properties in those stores
Original Description
In this Solid js tutorial you'll learn how to manage state using stores.
🚀🥷🏼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