Skip to content

Commit 8bfd940

Browse files
authored
Merge pull request #10 from reduxjs/website
Website and initial docs
2 parents 7980bb9 + 11372e6 commit 8bfd940

30 files changed

+15579
-5313
lines changed

docs/introduction/getting-started.md

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
id: getting-started
3+
title: Getting Started with Angular Redux
4+
hide_title: true
5+
sidebar_label: Getting Started
6+
description: 'Introduction > Getting Started: First steps with Angular Redux'
7+
---
8+
9+
# Getting Started with Angular Redux
10+
11+
[Angular Redux](https://github.com/reduxjs/angular-redux) is the official [Angular](https://angular.dev/) UI bindings layer for [Redux](https://redux.js.org/). It lets your Angular components read data from a Redux store, and dispatch actions to the store to update state.
12+
13+
## Installation
14+
15+
Angular Redux 8.x requires **Angular 17.3 or later**, in order to make use of Angular Signals.
16+
17+
### Installing with `ng add`
18+
19+
You can install the Store to your project with the following `ng add` command <a href="https://angular.dev/cli/add" target="_blank">(details here)</a>:
20+
21+
```sh
22+
ng add @reduxjs/angular-redux@latest
23+
```
24+
25+
#### Optional `ng add` flags
26+
27+
| flag | description | value type | default value |
28+
|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------|---------------|
29+
| `--path` | Path to the module that you wish to add the import for the `provideRedux` to. | `string` ||
30+
| `--project` | Name of the project defined in your `angular.json` to help locating the module to add the `provideRedux` to. | `string` ||
31+
| `--module` | Name of file containing the module that you wish to add the import for the `provideRedux` to. Can also include the relative path to the file. For example, `src/app/app.module.ts`. | `string` | `app` |
32+
| `--storePath` | The file path to create the state in. | `string` | `store` |
33+
34+
This command will automate the following steps:
35+
36+
1. Update `package.json` > `dependencies` with Redux, Redux Toolkit, and Angular Redux
37+
2. Run `npm install` to install those dependencies.
38+
3. Update your `src/app/app.module.ts` > `imports` array with `provideRedux({store})`
39+
4. If the project is using a `standalone bootstrap`, it adds `provideRedux({store})` into the application config.
40+
41+
### Installing with `npm` or `yarn`
42+
43+
To use Angular Redux with your Angular app, install it as a dependency:
44+
45+
```bash
46+
# If you use npm:
47+
npm install @reduxjs/angular-redux
48+
49+
# Or if you use Yarn:
50+
yarn add @reduxjs/angular-redux
51+
```
52+
53+
54+
You'll also need to [install Redux](https://redux.js.org/introduction/installation) and [set up a Redux store](https://redux.js.org/recipes/configuring-your-store/) in your app.
55+
56+
Angular-Redux is written in TypeScript, so all types are automatically included.
57+
58+
## API Overview
59+
60+
### `provideRedux`
61+
62+
Angular Redux includes a `provideRedux` provider factory, which makes the Redux store available to the rest of your app:
63+
64+
```typescript
65+
import { bootstrapApplication } from '@angular/platform-browser';
66+
import { provideRedux } from "angular-redux";
67+
import { AppComponent } from './app/app.component';
68+
import { store } from './store'
69+
70+
bootstrapApplication(AppComponent, {
71+
providers: [
72+
provideRedux({ store })
73+
]
74+
});
75+
```
76+
77+
### Injectables
78+
79+
Angular Redux provides a pair of custom Angular injectable functions that allow your Angular components to interact with the Redux store.
80+
81+
`injectSelector` reads a value from the store state and subscribes to updates, while `injectDispatch` returns the store's `dispatch` method to let you dispatch actions.
82+
83+
```typescript
84+
import { Component } from '@angular/core'
85+
import { injectSelector, injectDispatch } from "@reduxjs/angular-redux";
86+
import { decrement, increment } from './store/counter-slice'
87+
import { RootState } from './store'
88+
89+
@Component({
90+
selector: 'app-root',
91+
standalone: true,
92+
template: `
93+
<button (click)="dispatch(increment())">
94+
Increment
95+
</button>
96+
<span>{{ count() }}</span>
97+
<button (click)="dispatch(decrement())">
98+
Decrement
99+
</button>
100+
`
101+
})
102+
export class AppComponent {
103+
count = injectSelector((state: RootState) => state.counter.value)
104+
dispatch = injectDispatch()
105+
increment = increment
106+
decrement = decrement
107+
}
108+
```
109+
110+
## Help and Discussion
111+
112+
The **[#redux channel](https://discord.gg/0ZcbPKXt5bZ6au5t)** of the **[Reactiflux Discord community](http://www.reactiflux.com)** is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - come join us!
113+
114+
You can also ask questions on [Stack Overflow](https://stackoverflow.com) using the **[#redux tag](https://stackoverflow.com/questions/tagged/redux)**.

docs/tutorials/quick-start.md

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
---
2+
id: quick-start
3+
title: Quick Start
4+
sidebar_label: Quick Start
5+
hide_title: true
6+
---
7+
8+
&nbsp;
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

Comments
 (0)