Skip to content

Commit 21876ce

Browse files
committed
docs: mostly finish migrating tutorials/quick-start to Angular
Except the code sample
1 parent 32cfe7b commit 21876ce

File tree

1 file changed

+59
-70
lines changed

1 file changed

+59
-70
lines changed

docs/tutorials/quick-start.md

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,47 @@ hide_title: true
77

88
 
99

10-
# React Redux Quick Start
10+
# Angular Redux Quick Start
1111

1212
:::tip What You'll Learn
1313

14-
- How to set up and use Redux Toolkit with React Redux
14+
- How to set up and use Redux Toolkit with Angular Redux
1515

1616
:::
1717

1818
:::info Prerequisites
1919

2020
- Familiarity with [ES6 syntax and features](https://www.taniarascia.com/es6-syntax-and-feature-overview/)
21-
- Knowledge of React terminology: [JSX](https://react.dev/learn/writing-markup-with-jsx), [State](https://react.dev/learn/state-a-components-memory), [Function Components, Props](https://react.dev/learn/passing-props-to-a-component), and [Hooks](https://react.dev/reference/react#)
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)
2222
- Understanding of [Redux terms and concepts](https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow)
2323

2424
:::
2525

2626
## Introduction
2727

28-
Welcome to the React Redux Quick Start tutorial! **This tutorial will briefly introduce you to React Redux and teach you how to start using it correctly**.
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**.
2929

3030
### How to Read This Tutorial
3131

3232
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).
3333

34-
For this tutorial, we assume that you're using Redux Toolkit and React Redux together, as that is the standard Redux usage pattern. The examples are based on [a typical Create-React-App folder structure](https://create-react-app.dev/docs/folder-structure) 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-
The [Redux+JS template for Create-React-App](https://github.com/reduxjs/cra-template-redux) comes with this same project setup already configured.
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.
3735

3836
## Usage Summary
3937

40-
### Install Redux Toolkit and React Redux
38+
### Install Redux Toolkit and Angular Redux
4139

42-
Add the Redux Toolkit and React Redux packages to your project:
40+
Add the Redux Toolkit and Angular Redux packages to your project:
4341

4442
```sh
45-
npm install @reduxjs/toolkit react-redux
43+
npm install @reduxjs/toolkit angular-redux
4644
```
4745

4846
### Create a Redux Store
4947

5048
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:
5149

52-
```js title="app/store.js"
50+
```typescript title="app/store.js"
5351
import { configureStore } from '@reduxjs/toolkit'
5452

5553
export default configureStore({
@@ -59,29 +57,25 @@ export default configureStore({
5957

6058
This creates a Redux store, and also automatically configure the Redux DevTools extension so that you can inspect the store while developing.
6159

62-
### Provide the Redux Store to React
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:
6363

64-
Once the store is created, we can make it available to our React components by putting a React Redux `<Provider>` around our application in `src/index.js`. Import the Redux store we just created, put a `<Provider>` around your `<App>`, and pass the store as a prop:
64+
```typescript title="main.ts"
6565

66-
```js title="index.js"
67-
import React from 'react'
68-
import ReactDOM from 'react-dom/client'
69-
import './index.css'
70-
import App from './App'
66+
import { bootstrapApplication } from '@angular/platform-browser';
67+
import { AppComponent } from './app/app.component';
7168
// highlight-start
72-
import store from './app/store'
73-
import { Provider } from 'react-redux'
69+
import { provideRedux } from "angular-redux";
70+
import { store } from './store'
7471
// highlight-end
7572

76-
// As of React 18
77-
const root = ReactDOM.createRoot(document.getElementById('root'))
78-
79-
root.render(
80-
// highlight-next-line
81-
<Provider store={store}>
82-
<App />
83-
</Provider>,
84-
)
73+
bootstrapApplication(AppComponent, {
74+
providers: [
75+
// highlight-next-line
76+
provideRedux({ store })
77+
]
78+
});
8579
```
8680

8781
### Create a Redux State Slice
@@ -141,67 +135,62 @@ export default configureStore({
141135
})
142136
```
143137

144-
### Use Redux State and Actions in React Components
145-
146-
Now we can use the React Redux hooks to let React 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.js` file with a `<Counter>` component inside, then import that component into `App.js` and render it inside of `<App>`.
147-
148-
```jsx title="features/counter/Counter.js"
149-
import React from 'react'
150-
import { useSelector, useDispatch } from 'react-redux'
151-
import { decrement, increment } from './counterSlice'
152-
import styles from './Counter.module.css'
153-
154-
export function Counter() {
155-
const count = useSelector((state) => state.counter.value)
156-
const dispatch = useDispatch()
157-
158-
return (
159-
<div>
160-
<div>
161-
<button
162-
aria-label="Increment value"
163-
onClick={() => dispatch(increment())}
164-
>
165-
Increment
166-
</button>
167-
<span>{count}</span>
168-
<button
169-
aria-label="Decrement value"
170-
onClick={() => dispatch(decrement())}
171-
>
172-
Decrement
173-
</button>
174-
</div>
175-
</div>
176-
)
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
177166
}
178167
```
179168

180169
Now, any time you click the "Increment" and "Decrement buttons:
181170

182171
- The corresponding Redux action will be dispatched to the store
183172
- The counter slice reducer will see the actions and update its state
184-
- The `<Counter>` component will see the new state value from the store and re-render itself with the new data
173+
- The `<app-counter>` component will see the new state value from the store and re-render itself with the new data
185174

186175
## What You've Learned
187176

188-
That was a brief overview of how to set up and use Redux Toolkit with React. Recapping the details:
177+
That was a brief overview of how to set up and use Redux Toolkit with Angular. Recapping the details:
189178

190179
:::tip Summary
191180

192181
- **Create a Redux store with `configureStore`**
193182
- `configureStore` accepts a `reducer` function as a named argument
194183
- `configureStore` automatically sets up the store with good default settings
195-
- **Provide the Redux store to the React application components**
196-
- Put a React Redux `<Provider>` component around your `<App />`
184+
- **Provide the Redux store to the Angular application components**
185+
- Put a Angular Redux `provideRedux` provider factory in your `bootstrapApplication`'s `providers` array
197186
- Pass the Redux store as `<Provider store={store}>`
198187
- **Create a Redux "slice" reducer with `createSlice`**
199188
- Call `createSlice` with a string name, an initial state, and named reducer functions
200189
- Reducer functions may "mutate" the state using Immer
201190
- Export the generated slice reducer and action creators
202-
- **Use the React Redux `useSelector/useDispatch` hooks in React components**
203-
- Read data from the store with the `useSelector` hook
204-
- Get the `dispatch` function with the `useDispatch` hook, and dispatch actions as needed
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
205194

206195
:::
207196

@@ -219,4 +208,4 @@ Here's the complete Counter application as a running CodeSandbox:
219208
220209
## What's Next?
221210

222-
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 React Redux do, and how to use it correctly.
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)