Vuex 4 & Firebase Auth Tutorial #3 - Vuex & the Composition API

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

Key Takeaways

This video tutorial demonstrates how to use Vuex 4 with the Composition API in Vue.js, specifically using the useStore function to access the store and its state and mutations. The tutorial shows how to replace the Options API with the Composition API, using the setup hook to define reactive values and methods.

Full Transcript

so then gang now we've seen the basics of how view x works how we can make a store add states and mutations and then access all of this from inside components and at the minute we do all of this using the options api which is the slightly older way of working with vue.js where we define methods and computed values like this as properties on our component object now i'd like to show you how to do this using the composition api instead which doesn't use these properties but instead uses a setup hook and then inside that setup hook we can place all of our reactive values and methods and then return the ones that we need to use in the template at the end of it now again at this point i'm assuming you already know a bit about working with vue this way using the composition api if not definitely check out my view three courses first of all because i talk about them extensively in those the links are going to be down below anyway the way we work with vuex using the composition api is by using a function new in view x version 4 called use store and this gives us access to the store and the state and mutations on that store so the first thing we need to do is import that function from the view x library and like i said this is brand new to view x4 so make sure you have that version installed for this to work so what i'd like to do then is basically keep all of the same functionality we're still going to get the points from the store and we're still going to commit this mutation right here but we're going to do it all inside the setup hook using this function right here use store now the reason we need this function ustor is because we need to use that function to get access to the store itself now before when we use the composition api down here we could say this dot store but we don't have access to this inside the setup function so we can't do that in here and that's why we use this instead because this function returns to us the store so i'm going to say down here const and i'll call it store and set it equal to use store like so and what that does is get us access to the store so now this is basically the same as this thing right here and we can use the same different properties and methods on it so i could grab the points i could say const points is equal to something now it's a computed property much like we used a computer property down here so we need to say computed and also we need to import that from view as well this is the way we work in the composition api we import the functions we need and then inside that computed function we basically just want to return a value and the value that we want to return is going to be the store which we have access to because of this right here dot state dot points so it's exactly the same as this down here the store.state.points only instead of using this dollar sign store we use this function to get the store and use the properties on this instead okay so now we can return the points down here so this now is doing exactly the same thing as this so now we can get rid of this computed property we don't need that anymore all right so next let's do this method so same again we just need to create a function so i'm going to call this update points again and we set it equal to a function we're going to take in the points p call it what you want i called it points down here but i'm calling it p up here so as not to get this p confused with the points we have right here so then inside the function all we need to do is say store dot commit and then whatever mutation we want to commit which is update points in our case and then pass in p as the second argument that's the payload all right and then we can return that down here update points we can spell it update points like so and now this does exactly the same thing as this we take the store we commit a mutation and we pass in the payload p and that's what we did down here okay so now we can get rid of this methods thing as well like so and now all of this should work exactly the same way only now we're using the composition api and doing everything inside the setup hook so first of all we get the store using this use store function then we create this points computed property which is accessing the points from the state on the store and then we create a function called update points we're taking an argument p to be the new points and then we commit a new mutation on the store called update points and pass in the new value of p and remember that is this mutation right here which is updating our state so now we've returned all these down here which is still being used up here so let's save this and give it a whirl all right so we can see the state initially is zero for the points if we add a point it should go up by one each time which it does awesome and if we click this one it goes down by a point each time awesome so this all works so it's doing exactly the same thing only this time we've used the composition api and that use store function from the ux to interact with the store and this is only available in view x version 4 because it was made to be used with view 3 which is heavily involved with the composition api so make sure you're using vuex version 4 in order to use this use store function now for the rest of this series we're going to be using the composition api and using the store this way

Original Description

Hey gang, in this Vuex 4 tutorialI'll show you how to use our Vuex store in the setup( ) hook, using the composition API. 🔥🔥 Get the full course now on Net Ninja Pro 👉 https://netninja.dev/p/vuex-with-firebase-auth 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/Vuex-4-Firebase-Auth 🐱‍💻 Starter Project: https://github.com/iamshaunjp/Vuex-4-Firebase-Auth/tree/lesson-1 🐱‍💻 Vue 3 & Firebase Complete Course: On Net Ninja Pro - https://netninja.dev/p/build-websites-with-vue-3-firebase On Udemy - https://thenetninja.co.uk/udemy/vue-and-firebase 🐱‍💻 Vue 3 Basics Course: On YouTube - https://www.youtube.com/watch?v=YrxBCBibVo0&list=PL4cUxeGkcC9hYYGbV60Vq3IXYNfDk8At1 🐱‍💻 Getting Started with Firebase Course: On Net Ninja Pro - https://netninja.dev/p/getting-started-with-firebase-9 On YouTube - https://www.youtube.com/watch?v=9zdvmgGsww0&list=PL4cUxeGkcC9jERUGvbudErNCeSZHWUVlb 🐱‍👤 My Premium Udemy courses: https://thenetninja.co.uk/udemy/react-and-firebase https://thenetninja.co.uk/udemy/vue-and-firebase https://thenetninja.co.uk/udemy/modern-javascript https://thenetninja.co.uk/udemy/d3-and-firebase 🐱‍👤 Get access to all other premium courses on Net Ninja Pro: https://netninja.dev/ 🐱‍💻 My Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
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 tutorial teaches how to use Vuex 4 with the Composition API in Vue.js, allowing developers to manage state and mutations in a more efficient way. It covers the use of the useStore function and the setup hook to define reactive values and methods.

Key Takeaways
  1. Import the useStore function from the Vuex library
  2. Use the useStore function to get access to the store
  3. Define a computed property to access the state
  4. Create a function to commit a mutation
  5. Return the computed property and the function from the setup hook
💡 The useStore function is a new feature in Vuex 4 that allows developers to access the store and its state and mutations using the Composition API.

Related Reads

📰
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Learn the 5 levels of front end engineering to improve your skills and avoid getting stuck in a career rut
Medium · Programming
📰
Browser-Based PDF Editing with Vue 3 and pdf-lib
Learn to build a browser-based PDF editor using Vue 3 and pdf-lib, enabling users to edit PDFs directly in the browser
Dev.to · sunshey
📰
Say Goodbye To Electron?
Learn about a new approach to building native applications without Electron, using frontend-style development
Medium · Programming
📰
Why Vanilla JS? In the article below, I am sharing my story of building SaaS product in vanilla js and explaining why I decided to go with this approach. https://guseyn.com/html/posts/why-vanilla-js.html
Learn why choosing Vanilla JS can be a great approach for building SaaS products and how it can simplify development
Dev.to · Guseyn Ismayylov
Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →