|
| 1 | +--- |
| 2 | +id: quick-start |
| 3 | +title: Quick Start |
| 4 | +sidebar_label: Quick Start |
| 5 | +hide_title: true |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +# Vue Redux Quick Start |
| 11 | + |
| 12 | +:::tip What You'll Learn |
| 13 | + |
| 14 | +- How to set up and use Redux Toolkit with Vue Redux |
| 15 | + |
| 16 | +::: |
| 17 | + |
| 18 | +:::info Prerequisites |
| 19 | + |
| 20 | +- Familiarity with [ES6 syntax and features](https://www.taniarascia.com/es6-syntax-and-feature-overview/) |
| 21 | +- Knowledge of Vue terminology: [State](https://vuejs.org/guide/scaling-up/state-management), [Components, Props](https://vuejs.org/guide/essentials/component-basics), and [the Composition API](https://vuejs.org/guide/extras/composition-api-faq.html) |
| 22 | +- Understanding of [Redux terms and concepts](https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow) |
| 23 | + |
| 24 | +::: |
| 25 | + |
| 26 | +## Introduction |
| 27 | + |
| 28 | +Welcome to the Vue Redux Quick Start tutorial! **This tutorial will briefly introduce you to Vue Redux and teach you how to start using it correctly**. |
| 29 | + |
| 30 | +### How to Read This Tutorial |
| 31 | + |
| 32 | +This page will focus on just how to set up a Redux application with Redux Toolkit and the main APIs you'll use. For explanations of what Redux is, how it works, and full examples of how to use Redux Toolkit, see [the Redux core docs tutorials](https://redux.js.org/tutorials/index). |
| 33 | + |
| 34 | +For this tutorial, we assume that you're using Redux Toolkit and Vue Redux together, as that is the standard Redux usage pattern. The examples are based on [a typical Vite Vue CLI folder structure](https://vite.dev/guide/) where all the application code is in a `src`, but the patterns can be adapted to whatever project or folder setup you're using. |
| 35 | + |
| 36 | +## Usage Summary |
| 37 | + |
| 38 | +### Install Redux Toolkit and Vue Redux |
| 39 | + |
| 40 | +Add the Redux Toolkit and Vue Redux packages to your project: |
| 41 | + |
| 42 | +```sh |
| 43 | +npm install @reduxjs/toolkit @reduxjs/vue-redux |
| 44 | +``` |
| 45 | + |
| 46 | +### Create a Redux Store |
| 47 | + |
| 48 | +Create a file named `src/app/store.ts`. Import the `configureStore` API from Redux Toolkit. We'll start by creating an empty Redux store, and exporting it: |
| 49 | + |
| 50 | +```typescript title="app/store.ts" |
| 51 | +import { configureStore } from "@reduxjs/toolkit"; |
| 52 | + |
| 53 | +export default configureStore({ |
| 54 | + reducer: {}, |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +This creates a Redux store, and also automatically configure the Redux DevTools extension so that you can inspect the store while developing. |
| 59 | + |
| 60 | +### Provide the Redux Store to Vue |
| 61 | + |
| 62 | +Once the store is created, we can make it available to our Vue components by putting an Vue Redux `provideStoreToApp` function call to wrap our `createApp` instance in `src/main.ts`. Import the Redux store we just created, put a `provideStoreToApp` call around your `createApp` instance, and pass the store as a prop: |
| 63 | + |
| 64 | +```typescript title="main.ts" |
| 65 | +import { createApp } from 'vue' |
| 66 | +import App from './App.vue' |
| 67 | +// highlight-start |
| 68 | +import { provideStoreToApp } from '@reduxjs/vue-redux' |
| 69 | +import { store } from './store' |
| 70 | +// highlight-end |
| 71 | + |
| 72 | +const app = createApp(App) |
| 73 | +// highlight-next-line |
| 74 | +provideStoreToApp(app, { store }) |
| 75 | +app.mount('#app') |
| 76 | +``` |
| 77 | + |
| 78 | +### Create a Redux State Slice |
| 79 | + |
| 80 | +Add a new file named `src/features/counter/counter-slice.ts`. In that file, import the `createSlice` API from Redux Toolkit. |
| 81 | + |
| 82 | +Creating a slice requires a string name to identify the slice, an initial state value, and one or more reducer functions to define how the state can be updated. Once a slice is created, we can export the generated Redux action creators and the reducer function for the whole slice. |
| 83 | + |
| 84 | +Redux requires that [we write all state updates immutably, by making copies of data and updating the copies](https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow#immutability). However, Redux Toolkit's `createSlice` and `createReducer` APIs use [Immer](https://immerjs.github.io/immer/) inside to allow us to [write "mutating" update logic that becomes correct immutable updates](https://redux.js.org/tutorials/fundamentals/part-8-modern-redux#immutable-updates-with-immer). |
| 85 | + |
| 86 | +```js title="features/counter/counter-slice.ts" |
| 87 | +import { createSlice } from "@reduxjs/toolkit"; |
| 88 | + |
| 89 | +export const counterSlice = createSlice({ |
| 90 | + name: "counter", |
| 91 | + initialState: { |
| 92 | + value: 0, |
| 93 | + }, |
| 94 | + reducers: { |
| 95 | + increment: (state) => { |
| 96 | + // Redux Toolkit allows us to write "mutating" logic in reducers. It |
| 97 | + // doesn't actually mutate the state because it uses the Immer library, |
| 98 | + // which detects changes to a "draft state" and produces a brand new |
| 99 | + // immutable state based off those changes. |
| 100 | + // Also, no return statement is required from these functions. |
| 101 | + state.value += 1; |
| 102 | + }, |
| 103 | + decrement: (state) => { |
| 104 | + state.value -= 1; |
| 105 | + }, |
| 106 | + incrementByAmount: (state, action) => { |
| 107 | + state.value += action.payload; |
| 108 | + }, |
| 109 | + }, |
| 110 | +}); |
| 111 | + |
| 112 | +// Action creators are generated for each case reducer function |
| 113 | +export const { increment, decrement, incrementByAmount } = counterSlice.actions; |
| 114 | + |
| 115 | +export default counterSlice.reducer; |
| 116 | +``` |
| 117 | + |
| 118 | +### Add Slice Reducers to the Store |
| 119 | + |
| 120 | +Next, we need to import the reducer function from the counter slice and add it to our store. By defining a field inside the `reducer` parameter, we tell the store to use this slice reducer function to handle all updates to that state. |
| 121 | + |
| 122 | +```js title="app/store.ts" |
| 123 | +import { configureStore } from '@reduxjs/toolkit' |
| 124 | +// highlight-next-line |
| 125 | +import counterReducer from './features/counter/counterSlice' |
| 126 | + |
| 127 | +export default configureStore({ |
| 128 | + reducer: { |
| 129 | + // highlight-next-line |
| 130 | + counter: counterReducer, |
| 131 | + }, |
| 132 | +}) |
| 133 | +``` |
| 134 | + |
| 135 | +### Use Redux State and Actions in Vue Components |
| 136 | + |
| 137 | +Now we can use the Vue Redux compositions to let Vue components interact with the Redux store. We can read data from the store with `useSelector`, and dispatch actions using `useDispatch`. Create a `src/features/counter/counter.component.ts` file with a `<Counter>` component inside, then import that component into `App.vue` and render it inside of `<App>`. |
| 138 | + |
| 139 | +```vue title="features/counter/Counter.vue" |
| 140 | +<script setup> |
| 141 | + import { useSelector, useDispatch } from '@reduxjs/vue-redux' |
| 142 | + import { decrement, increment } from './counter-slice' |
| 143 | +
|
| 144 | + const count = useSelector((state) => state.counter.value) |
| 145 | + const dispatch = useDispatch() |
| 146 | +</script> |
| 147 | +
|
| 148 | +<template> |
| 149 | + <div> |
| 150 | + <div> |
| 151 | + <button aria-label="Increment value" @click="dispatch(increment())"> |
| 152 | + Increment |
| 153 | + </button> |
| 154 | + <span>{{ count }}</span> |
| 155 | + <button aria-label="Decrement value" @click="dispatch(decrement())"> |
| 156 | + Decrement |
| 157 | + </button> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | +</template> |
| 161 | +``` |
| 162 | + |
| 163 | +Now, any time you click the "Increment" and "Decrement buttons: |
| 164 | + |
| 165 | +- The corresponding Redux action will be dispatched to the store |
| 166 | +- The counter slice reducer will see the actions and update its state |
| 167 | +- The `<Counter>` component will see the new state value from the store and re-render itself with the new data |
| 168 | + |
| 169 | +## What You've Learned |
| 170 | + |
| 171 | +That was a brief overview of how to set up and use Redux Toolkit with Vue. Recapping the details: |
| 172 | + |
| 173 | +:::tip Summary |
| 174 | + |
| 175 | +- **Create a Redux store with `configureStore`** |
| 176 | + - `configureStore` accepts a `reducer` function as a named argument |
| 177 | + - `configureStore` automatically sets up the store with good default settings |
| 178 | +- **Provide the Redux store to the Vue application components** |
| 179 | + - Put a Vue Redux `provideStoreToApp` function to wrap your `createApp`'s instance |
| 180 | +- **Create a Redux "slice" reducer with `createSlice`** |
| 181 | + - Call `createSlice` with a string name, an initial state, and named reducer functions |
| 182 | + - Reducer functions may "mutate" the state using Immer |
| 183 | + - Export the generated slice reducer and action creators |
| 184 | +- **Use the Vue Redux `useSelector/useDispatch` compositions in Vue components** |
| 185 | + - Read data from the store with the `useSelector` composition |
| 186 | + - Get the `dispatch` function with the `useDispatch` composition, and dispatch actions as needed |
| 187 | + |
| 188 | +::: |
| 189 | + |
| 190 | +### Full Counter App Example |
| 191 | + |
| 192 | +Here's the complete Counter application as a running StackBlitz: |
| 193 | + |
| 194 | +<iframe |
| 195 | + class="codesandbox" |
| 196 | + src="https://stackblitz.com/github/reduxjs/vue-redux-essentials-counter-example/tree/main?template=node&ctl=1&embed=1&file=src%2Fapp%2Ffeatures%2Fcounter%2Fcounter-slice.ts&hideNavigation=1&view=preview" |
| 197 | + title="vue-redux-essentials-example" |
| 198 | + allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" |
| 199 | + sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" |
| 200 | +></iframe> |
| 201 | +
|
| 202 | +## What's Next? |
| 203 | + |
| 204 | +We recommend going through [**the "Redux Essentials" and "Redux Fundamentals" tutorials in the Redux core docs**](https://redux.js.org/tutorials/index), which will give you a complete understanding of how Redux works, what Redux Toolkit and Vue Redux do, and how to use it correctly. |
0 commit comments