Skip to content

Commit 2996c9f

Browse files
committed
Add "Quick Start" pages and update installation instructions
1 parent 1aaf44d commit 2996c9f

File tree

5 files changed

+462
-57
lines changed

5 files changed

+462
-57
lines changed

docs/introduction/getting-started.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ React Redux 7.1+ requires **React 16.8.3 or later**, in order to make use of Rea
2121

2222
### Using Create React App
2323

24-
The recommended way to start new apps with React Redux is by using the [official Redux+JS template](https://github.com/reduxjs/cra-template-redux) for [Create React App](https://github.com/facebook/create-react-app), which takes advantage of [Redux Toolkit](https://redux-toolkit.js.org/).
24+
The recommended way to start new apps with React and Redux is by using the [official Redux+JS template](https://github.com/reduxjs/cra-template-redux) or [Redux+TS template](https://github.com/reduxjs/cra-template-redux-typescript) for [Create React App](https://github.com/facebook/create-react-app), which takes advantage of **[Redux Toolkit](https://redux-toolkit.js.org/)** and React Redux's integration with React components.
2525

26-
```sh
26+
```bash
27+
# Redux + Plain JS template
2728
npx create-react-app my-app --template redux
29+
30+
# Redux + TypeScript template
31+
npx create-react-app my-app --template redux-typescript
2832
```
2933

3034
### An Existing React App
@@ -41,14 +45,12 @@ yarn add react-redux
4145

4246
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.
4347

44-
If you are using TypeScript, the React Redux types are maintained separately in DefinitelyTyped. You'll need to install those as well:
48+
If you are using TypeScript, the React Redux types are maintained separately in DefinitelyTyped, but included as a dependency of the `react-redux` package, so they should be installed automatically. If you still need to install them manually, run:
4549

4650
```bash
4751
npm install @types/react-redux
4852
```
4953

50-
The code used for this example is based on the [official Redux template](https://github.com/reduxjs/cra-template-redux). Additionally, the same code template for TypeScript can be found [here](https://github.com/reduxjs/cra-template-redux-typescript).
51-
5254
## API Overview
5355

5456
### `Provider`

docs/tutorials/quick-start.md

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
---
2+
id: quick-start
3+
title: Quick Start
4+
sidebar_label: Quick Start
5+
hide_title: true
6+
---
7+
8+
 
9+
10+
# React Redux Quick Start
11+
12+
:::tip What You'll Learn
13+
14+
- How to set up and use Redux Toolkit with React 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 React terminology: [JSX](https://reactjs.org/docs/introducing-jsx.html), [State](https://reactjs.org/docs/state-and-lifecycle.html), [Function Components, Props](https://reactjs.org/docs/components-and-props.html), and [Hooks](https://reactjs.org/docs/hooks-intro.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 React Redux Quick Start tutorial! **This tutorial will briefly introduce you to React 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 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.
37+
38+
## Usage Summary
39+
40+
### Install Redux Toolkit and React Redux
41+
42+
Add the Redux Toolkit and React Redux packages to your project:
43+
44+
```sh
45+
npm install @reduxjs/toolkit react-redux
46+
```
47+
48+
### Create a Redux Store
49+
50+
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:
51+
52+
```js title="app/store.js"
53+
import { configureStore } from '@reduxjs/toolkit'
54+
55+
export default configureStore({
56+
reducer: {},
57+
})
58+
```
59+
60+
This creates a Redux store, and also automatically configure the Redux DevTools extension so that you can inspect the store while developing.
61+
62+
### Provide the Redux Store to React
63+
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:
65+
66+
```js title="index.js"
67+
import React from 'react'
68+
import ReactDOM from 'react-dom'
69+
import './index.css'
70+
import App from './App'
71+
// highlight-start
72+
import store from './app/store'
73+
import { Provider } from 'react-redux'
74+
// highlight-end
75+
76+
ReactDOM.render(
77+
// highlight-next-line
78+
<Provider store={store}>
79+
<App />
80+
</Provider>,
81+
document.getElementById('root')
82+
)
83+
```
84+
85+
### Create a Redux State Slice
86+
87+
Add a new file named `src/features/counter/counterSlice.js`. In that file, import the `createSlice` API from Redux Toolkit.
88+
89+
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.
90+
91+
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).
92+
93+
```js title="features/counter/counterSlice.js"
94+
import { createSlice } from '@reduxjs/toolkit'
95+
96+
export const counterSlice = createSlice({
97+
name: 'counter',
98+
initialState: {
99+
value: 0,
100+
},
101+
reducers: {
102+
increment: (state) => {
103+
// Redux Toolkit allows us to write "mutating" logic in reducers. It
104+
// doesn't actually mutate the state because it uses the Immer library,
105+
// which detects changes to a "draft state" and produces a brand new
106+
// immutable state based off those changes
107+
state.value += 1
108+
},
109+
decrement: (state) => {
110+
state.value -= 1
111+
},
112+
incrementByAmount: (state, action) => {
113+
state.value += action.payload
114+
},
115+
},
116+
})
117+
118+
// Action creators are generated for each case reducer function
119+
export const { increment, decrement, incrementByAmount } = counterSlice.actions
120+
121+
export default counterSlice.reducer
122+
```
123+
124+
### Add Slice Reducers to the Store
125+
126+
Next, we need to import the reducer function from the counter slice and add it to our store. By defining a field inside the `reducers` parameter, we tell the store to use this slice reducer function to handle all updates to that state.
127+
128+
```js title="app/store.js"
129+
import { configureStore } from '@reduxjs/toolkit'
130+
// highlight-next-line
131+
import counterReducer from '../features/counter/counterSlice'
132+
133+
export default configureStore({
134+
reducer: {
135+
// highlight-next-line
136+
counter: counterReducer,
137+
},
138+
})
139+
```
140+
141+
### Use Redux State and Actions in React Components
142+
143+
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>`.
144+
145+
```jsx title="features/counter/Counter.js"
146+
import React, { useState } from 'react'
147+
import { useSelector, useDispatch } from 'react-redux'
148+
import { decrement, increment } from './counterSlice'
149+
import styles from './Counter.module.css'
150+
151+
export function Counter() {
152+
const count = useSelector((state) => state.counter.value)
153+
const dispatch = useDispatch()
154+
155+
return (
156+
<div>
157+
<div>
158+
<button
159+
aria-label="Increment value"
160+
onClick={() => dispatch(increment())}
161+
>
162+
Increment
163+
</button>
164+
<span>{count}</span>
165+
<button
166+
aria-label="Decrement value"
167+
onClick={() => dispatch(decrement())}
168+
>
169+
Decrement
170+
</button>
171+
</div>
172+
</div>
173+
)
174+
}
175+
```
176+
177+
Now, any time you click the "Increment" and "Decrement buttons:
178+
179+
- The corresponding Redux action will be dispatched to the store
180+
- The counter slice reducer will see the actions and update its state
181+
- The `<Counter>` component will see the new state value from the store and re-render itself with the new data
182+
183+
## What You've Learned
184+
185+
That was a brief overview of how to set up and use Redux Toolkit with React. Recapping the details:
186+
187+
:::tip Summary
188+
189+
- **Create a Redux store with `configureStore`**
190+
- `configureStore` accepts a `reducer` function as a named argument
191+
- `configureStore` automatically sets up the store with good default settings
192+
- **Provide the Redux store to the React application components**
193+
- Put a React Redux `<Provider>` component around your `<App />`
194+
- Pass the Redux store as `<Provider store={store}>`
195+
- **Create a Redux "slice" reducer with `createSlice`**
196+
- Call `createSlice` with a string name, an initial state, and named reducer functions
197+
- Reducer functions may "mutate" the state using Immer
198+
- Export the generated slice reducer and action creators
199+
- **Use the React Redux `useSelector/useDispatch` hooks in React components**
200+
- Read data from the store with the `useSelector` hook
201+
- Get the `dispatch` function with the `useDispatch` hook, and dispatch actions as needed
202+
203+
:::
204+
205+
### Full Counter App Example
206+
207+
The counter example app shown here is also the
208+
209+
Here's the complete counter application as a running CodeSandbox:
210+
211+
<iframe
212+
class="codesandbox"
213+
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"
214+
title="redux-essentials-example"
215+
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
216+
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
217+
></iframe>
218+
219+
## What's Next?
220+
221+
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.

0 commit comments

Comments
 (0)