Solid JS Tutorial #14 - Context

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

Key Takeaways

The video tutorial demonstrates how to use Context in Solid JS to create global state, utilizing tools like Solid and React, and covering concepts such as context, state management, and component-based architecture. It provides a step-by-step guide on creating a new component to hold the store, using the createContext function, and accessing the context value in other components.

Full Transcript

okay then my friends so in this lesson I want to make some states which is going to keep track of any products that we add to the shopping cart now on each product page I'm going to add a little button and when we click on that button that's when we want to add a product that product to the car and the way that we'll do that is by making a store to keep track of all of those products that we've added to the car so the store value will be essentially just an array of product objects and every time we click on a button to add a new product to the cart a new object is going to get added to the store but we need to think a little bit about how this is going to work because we're going to need to access this store on various Pages we're going to need it on the product page so that we can add new products to the store but also on the cart page so that we can access all the products in the store and list them out also in the header we'll eventually be showing how many items are in the car as well so we need to access the store inside the header so then if we need to access the store in multiple pages and components in the application where's the best place to make the store well there's two options we could just make a store in a new file export it and then just use in all the different locations we need in because stores and signals don't have to be tied to specific components so we could do that but instead I want to take this opportunity to talk about context in solid JS because a context and a context provider can allow different pages and components to share state so the way this works is very similar to how it works in react applications we make a new component and inside that component we make whatever state is that we want to share between other components in our case that would be the store which holds an array of products in the cart so this component at the minute is just like any other solid component that contains a store inside it next we make a new context object using one of solid's built-in functions and on that context object we have access to a special provider component which is used to provide the context to other components so inside our own custom component we made we return this context provided component in the template and then we wrap that around props.children and that means that when this component that we just made wraps any other components in our application those other components are the props.children and they get output right here nested inside the provider component so whatever components are inside here now will now be provided with values that they can use and the values that they can use are determined by the value attribute right here so in our case that would be our store and our set store function so this component could wrap our root app component and therefore it's wrapping our entire application and that means that any component in our application will be able to access this context value this store so our product page could access it the cart page could access it and any other component inside the app could access it and the way it accepts it is by using a function called use context so we'll see that later but for now let's set up our context to look a little bit like this so the first thing I'm going to do is create a new folder inside source and that folder is going to be called context and any context files we create are going to go inside here so new file and we'll call this cart context.jsx and inside here we pretty much need to do everything we just did in a solid playground so let me go over there and I'm going to copy all of this stuff right here and paste it in and we also need to import a couple of things as well so these two things at the top so let me grab those and we're going to paste those in as well so we're creating the cart context right here using this function create context that we import and then we create this function which is a component called cart context provider and we're taking the props we set up a store which is an empty array at the minute we will add an item in that in a second then we return a template which is the cart context provider which comes from this and we pass a value to everything now at the minute we get an arrow right here and that's because stupidly I've tried to return two values here and they should be wrapped in an object so their properties on an object so now we have an items property on that object and the set items property which are these two things right here so we wrap this around all the children and we need to export these things actually so I'll export this at the top this cart context because we might need it in another file and also this one we do need this in another file because this is going to wrap our app component inside the index.jsx file so I think that's pretty much it for now let's save this and let's go to index.jsx and down here in between the router and the app let's do the cart context provider this thing right here that's going to import it for us and let's put the closing tag after the app like so alright so now this app is being wrapped by this context provider meaning it's going to have access to these values right here so how do we do that how do we access those values in other components inside our project well the way we do this is by using a built-in function called use context so let me go to the cart page where we want to use that context and I'm going to use that function so we say const and then we destructure what we want back from the context in our case I want the items right here so I will say const items is equal to use context and invoke that now we need to import that from solid so up here we'll say import use context and that comes from solid JS like so okay so inside here we need to say what context we want to use well in our case that's this context right here the cart context and we export it so we can use it over here so we'll say cart context so we click on this to import it and now we're getting the items back from that context now at the minute that's going to be an empty array but we could add some kind of dummy element in here so I could do an object and we could do a title and we'll just say test product and then we'll do a quantity property which is going to be how many of these we've got in the cart and that could be two then we'll do an ID that can be a hundred doesn't really matter and then we'll do a price of the item which could be 15 again doesn't really matter so now we have this product inside the items array what we could do is use that product by grabbing the items and then cycling through the items down here so I'm actually going to paste this in because we've seen this kind of code before so we'll use this four components and we'll say for each items and then for each item we fire a function and grab that item we output a paragraph for each one where we output the item title the item price and the item quantity at the end okay so I'm going to save this now and see if this works let me go to a browser and okay we can see now we have that test product in the cart and there's a quantity of two here as well awesome so that's working however there's a better pattern of using context in a component because at the minute what we're doing is we're using this import right here let me just cut it and we're also using this input right here so we're importing two things to use the context right now what we could do instead is go back to this context file and we could make a little helper function which we export so let me say export function it's going to be called use cart context like so and all it's going to do is return use context and then we're going to pass in the cart context like so so we're doing the same thing right here as we are right here what that means is we can get rid of these Imports right here get rid of that and then over here we need to import the use context thing from solid so let me put that there and what we can do now is just use this thing right here use cart context inside the component now so I could paste this in in fact what I'll do is delete some of that so that I can click on this to import it so we're importing that one thing now and we invoke that and that's going to return the same things to us so const items is equal to use cart context and that's a bit easier in whatever component now that we want to use this in we just have to import the one function so you might see that pattern a little bit all right so now we have the items we're doing the same thing we're going to cycle through those yep that works what I'm going to do is add one more item so let me copy that and paste it down here we'll say test Product 2 quantity three ID 101 and the price is 25 save that see if it works I'm going to refresh yep we get a test product two as well awesome so that's how we can create a context and now we could use that context value in any component it's shared between the different components anywhere in the application we can use it so in the next lesson what we're going to do is we're going to access the context in the product page the product Details page and use the set items function to actually update this store value so we can add items to the cart

Original Description

In this Solid js tutorial you'll learn how to use Context to create global state. 🚀🥷🏼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 tutorial teaches how to use Context in Solid JS to create global state, providing a step-by-step guide on creating a new component to hold the store, using the createContext function, and accessing the context value in other components. It covers concepts such as context, state management, and component-based architecture, and provides practical examples using Solid JS and React.

Key Takeaways
  1. Create a new component to hold the store with an array of products in the cart
  2. Use the createContext function to create a new context object
  3. Wrap the cartContext component around props.children to provide the context to other components
  4. Use the useContext function to access the context value in other components
  5. Create a cart context using the createContext function
  6. Add a dummy product to the cart context and display it in the cart page
  7. Use a helper function to simplify the code and reduce imports
💡 Using Context in Solid JS allows for efficient state management and sharing of data between components, making it a powerful tool for building complex applications.

Related Reads

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