|
| 1 | +--- |
| 2 | +id: quick-start |
| 3 | +title: Quick Start |
| 4 | +sidebar_label: Quick Start |
| 5 | +hide_title: true |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +# Angular Redux Quick Start |
| 11 | + |
| 12 | +:::tip What You'll Learn |
| 13 | + |
| 14 | +- How to set up and use Redux Toolkit with Angular 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 Angular terminology: [State](https://angular.dev/essentials/managing-dynamic-data), [Components, Props](https://angular.dev/essentials/components), and [Signals](https://angular.dev/guide/signals) |
| 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 Angular Redux Quick Start tutorial! **This tutorial will briefly introduce you to Angular 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 Angular Redux together, as that is the standard Redux usage pattern. The examples are based on [a typical Angular CLI folder structure](https://angular.dev/tools/cli) 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 Angular Redux |
| 39 | + |
| 40 | +Add the Redux Toolkit and Angular Redux packages to your project: |
| 41 | + |
| 42 | +```sh |
| 43 | +npm install @reduxjs/toolkit angular-redux |
| 44 | +``` |
| 45 | + |
| 46 | +### Create a Redux Store |
| 47 | + |
| 48 | +Create a file named `src/app/store.js`. 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.js" |
| 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 Angular |
| 61 | + |
| 62 | +Once the store is created, we can make it available to our Angular components by putting an Angular Redux `provideRedux` in our application's `providers` array in `src/main.ts`. Import the Redux store we just created, put a `provideRedux` in your application's `providers` array, and pass the store as a prop: |
| 63 | + |
| 64 | +```typescript title="main.ts" |
| 65 | + |
| 66 | +import { bootstrapApplication } from '@angular/platform-browser'; |
| 67 | +import { AppComponent } from './app/app.component'; |
| 68 | +// highlight-start |
| 69 | +import { provideRedux } from "angular-redux"; |
| 70 | +import { store } from './store' |
| 71 | +// highlight-end |
| 72 | + |
| 73 | +bootstrapApplication(AppComponent, { |
| 74 | + providers: [ |
| 75 | + // highlight-next-line |
| 76 | + provideRedux({ store }) |
| 77 | + ] |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +### Create a Redux State Slice |
| 82 | + |
| 83 | +Add a new file named `src/features/counter/counterSlice.js`. In that file, import the `createSlice` API from Redux Toolkit. |
| 84 | + |
| 85 | +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. |
| 86 | + |
| 87 | +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). |
| 88 | + |
| 89 | +```js title="features/counter/counterSlice.js" |
| 90 | +import { createSlice } from '@reduxjs/toolkit' |
| 91 | + |
| 92 | +export const counterSlice = createSlice({ |
| 93 | + name: 'counter', |
| 94 | + initialState: { |
| 95 | + value: 0, |
| 96 | + }, |
| 97 | + reducers: { |
| 98 | + increment: (state) => { |
| 99 | + // Redux Toolkit allows us to write "mutating" logic in reducers. It |
| 100 | + // doesn't actually mutate the state because it uses the Immer library, |
| 101 | + // which detects changes to a "draft state" and produces a brand new |
| 102 | + // immutable state based off those changes. |
| 103 | + // Also, no return statement is required from these functions. |
| 104 | + state.value += 1 |
| 105 | + }, |
| 106 | + decrement: (state) => { |
| 107 | + state.value -= 1 |
| 108 | + }, |
| 109 | + incrementByAmount: (state, action) => { |
| 110 | + state.value += action.payload |
| 111 | + }, |
| 112 | + }, |
| 113 | +}) |
| 114 | + |
| 115 | +// Action creators are generated for each case reducer function |
| 116 | +export const { increment, decrement, incrementByAmount } = counterSlice.actions |
| 117 | + |
| 118 | +export default counterSlice.reducer |
| 119 | +``` |
| 120 | + |
| 121 | +### Add Slice Reducers to the Store |
| 122 | + |
| 123 | +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. |
| 124 | + |
| 125 | +```js title="app/store.js" |
| 126 | +import { configureStore } from '@reduxjs/toolkit' |
| 127 | +// highlight-next-line |
| 128 | +import counterReducer from '../features/counter/counterSlice' |
| 129 | + |
| 130 | +export default configureStore({ |
| 131 | + reducer: { |
| 132 | + // highlight-next-line |
| 133 | + counter: counterReducer, |
| 134 | + }, |
| 135 | +}) |
| 136 | +``` |
| 137 | + |
| 138 | +### Use Redux State and Actions in Angular Components |
| 139 | + |
| 140 | +Now we can use the Angular Redux inject functions to let Angular 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 `<app-counter>` component inside, then import that component into `app.component.ts` and render it inside of `<app-root>`. |
| 141 | + |
| 142 | +```typescript title="features/counter/counter.component.ts" |
| 143 | +import { Component } from '@angular/core' |
| 144 | +import { injectSelector, injectDispatch } from "@reduxjs/angular-redux"; |
| 145 | +import { decrement, increment } from './store/counter-slice' |
| 146 | +import { RootState } from './store' |
| 147 | + |
| 148 | +@Component({ |
| 149 | + selector: 'app-counter', |
| 150 | + standalone: true, |
| 151 | + template: ` |
| 152 | + <button (click)="dispatch(increment())"> |
| 153 | + Increment |
| 154 | + </button> |
| 155 | + <span>{{ count() }}</span> |
| 156 | + <button (click)="dispatch(decrement())"> |
| 157 | + Decrement |
| 158 | + </button> |
| 159 | + ` |
| 160 | +}) |
| 161 | +export class CounterComponent { |
| 162 | + count = injectSelector((state: RootState) => state.counter.value) |
| 163 | + dispatch = injectDispatch() |
| 164 | + increment = increment |
| 165 | + decrement = decrement |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +Now, any time you click the "Increment" and "Decrement buttons: |
| 170 | + |
| 171 | +- The corresponding Redux action will be dispatched to the store |
| 172 | +- The counter slice reducer will see the actions and update its state |
| 173 | +- The `<app-counter>` component will see the new state value from the store and re-render itself with the new data |
| 174 | + |
| 175 | +## What You've Learned |
| 176 | + |
| 177 | +That was a brief overview of how to set up and use Redux Toolkit with Angular. Recapping the details: |
| 178 | + |
| 179 | +:::tip Summary |
| 180 | + |
| 181 | +- **Create a Redux store with `configureStore`** |
| 182 | + - `configureStore` accepts a `reducer` function as a named argument |
| 183 | + - `configureStore` automatically sets up the store with good default settings |
| 184 | +- **Provide the Redux store to the Angular application components** |
| 185 | + - Put a Angular Redux `provideRedux` provider factory in your `bootstrapApplication`'s `providers` array |
| 186 | + - Pass the Redux store as `<Provider store={store}>` |
| 187 | +- **Create a Redux "slice" reducer with `createSlice`** |
| 188 | + - Call `createSlice` with a string name, an initial state, and named reducer functions |
| 189 | + - Reducer functions may "mutate" the state using Immer |
| 190 | + - Export the generated slice reducer and action creators |
| 191 | +- **Use the Angular Redux `injectSelector/injectDispatch` injections in Angular components** |
| 192 | + - Read data from the store with the `injectSelector` injection |
| 193 | + - Get the `dispatch` function with the `injectDispatch` injection, and dispatch actions as needed |
| 194 | + |
| 195 | +::: |
| 196 | + |
| 197 | +### Full Counter App Example |
| 198 | + |
| 199 | +Here's the complete Counter application as a running CodeSandbox: |
| 200 | + |
| 201 | +<iframe |
| 202 | + class="codesandbox" |
| 203 | + src="https://codesandbox.io/embed/github/reduxjs/redux-essentials-counter-example/tree/master/?fontsize=14&hidenavigation=1&module=%2Fsrc%2Ffeatures%2Fcounter%2FcounterSlice.js&theme=dark&runonclick=1" |
| 204 | + title="redux-essentials-example" |
| 205 | + allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" |
| 206 | + sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" |
| 207 | +></iframe> |
| 208 | +
|
| 209 | +## What's Next? |
| 210 | + |
| 211 | +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 Angular Redux do, and how to use it correctly. |
0 commit comments