Skip to content

Commit 80d9f40

Browse files
authored
Merge pull request #1110 from reduxjs/docs/rtk-video
2 parents d4d7c54 + 47be325 commit 80d9f40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+288
-140
lines changed

docs/api/configureStore.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: configureStore
55
hide_title: true
66
---
77

8+
 
9+
810
# `configureStore`
911

1012
A friendly abstraction over the standard Redux `createStore` function that adds good defaults

docs/api/createAction.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: createAction
55
hide_title: true
66
---
77

8+
 
9+
810
# `createAction`
911

1012
A helper function for defining a Redux [action](https://redux.js.org/basics/actions) type and creator.
@@ -21,7 +23,7 @@ const INCREMENT = 'counter/increment'
2123
function increment(amount: number) {
2224
return {
2325
type: INCREMENT,
24-
payload: amount
26+
payload: amount,
2527
}
2628
}
2729

@@ -63,8 +65,8 @@ const addTodo = createAction('todos/add', function prepare(text: string) {
6365
payload: {
6466
text,
6567
id: nanoid(),
66-
createdAt: new Date().toISOString()
67-
}
68+
createdAt: new Date().toISOString(),
69+
},
6870
}
6971
})
7072

@@ -95,7 +97,7 @@ import { createAction, createReducer } from '@reduxjs/toolkit'
9597
const increment = createAction<number>('counter/increment')
9698
const decrement = createAction<number>('counter/decrement')
9799

98-
const counterReducer = createReducer(0, builder => {
100+
const counterReducer = createReducer(0, (builder) => {
99101
builder.addCase(increment, (state, action) => state + action.payload)
100102
builder.addCase(decrement, (state, action) => state - action.payload)
101103
})
@@ -133,7 +135,7 @@ const counterReducer = createReducer(0, {
133135
[increment]: (state, action) => state + action.payload,
134136

135137
// You would need to use the action type explicitly instead.
136-
[INCREMENT]: (state, action) => state + action.payload
138+
[INCREMENT]: (state, action) => state + action.payload,
137139
})
138140
```
139141
@@ -178,7 +180,7 @@ const increment = createAction<number>('INCREMENT')
178180
export const epic = (actions$: Observable<Action>) =>
179181
actions$.pipe(
180182
filter(increment.match),
181-
map(action => {
183+
map((action) => {
182184
// action.payload can be safely used as number here (and will also be correctly inferred by TypeScript)
183185
// ...
184186
})

docs/api/createAsyncThunk.mdx

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: createAsyncThunk
55
hide_title: true
66
---
77

8+
&nbsp;
9+
810
# `createAsyncThunk`
911

1012
## Overview
@@ -42,8 +44,8 @@ const usersSlice = createSlice({
4244
[fetchUserById.fulfilled]: (state, action) => {
4345
// Add user to the state array
4446
state.entities.push(action.payload)
45-
}
46-
}
47+
},
48+
},
4749
})
4850

4951
// Later, dispatch the thunk as needed in the app
@@ -193,10 +195,10 @@ To handle these actions in your reducers, reference the action creators in `crea
193195
194196
```js {2,6,14,23}
195197
const reducer1 = createReducer(initialState, {
196-
[fetchUserById.fulfilled]: (state, action) => {}
198+
[fetchUserById.fulfilled]: (state, action) => {},
197199
})
198200

199-
const reducer2 = createReducer(initialState, builder => {
201+
const reducer2 = createReducer(initialState, (builder) => {
200202
builder.addCase(fetchUserById.fulfilled, (state, action) => {})
201203
})
202204

@@ -205,17 +207,17 @@ const reducer3 = createSlice({
205207
initialState,
206208
reducers: {},
207209
extraReducers: {
208-
[fetchUserById.fulfilled]: (state, action) => {}
209-
}
210+
[fetchUserById.fulfilled]: (state, action) => {},
211+
},
210212
})
211213

212214
const reducer4 = createSlice({
213215
name: 'users',
214216
initialState,
215217
reducers: {},
216-
extraReducers: builder => {
218+
extraReducers: (builder) => {
217219
builder.addCase(fetchUserById.fulfilled, (state, action) => {})
218-
}
220+
},
219221
})
220222
```
221223

@@ -244,8 +246,8 @@ import { unwrapResult } from '@reduxjs/toolkit'
244246
const onClick = () => {
245247
dispatch(fetchUserById(userId))
246248
.then(unwrapResult)
247-
.then(originalPromiseResult => {})
248-
.catch(rejectedValueOrSerializedError => {})
249+
.then((originalPromiseResult) => {})
250+
.catch((rejectedValueOrSerializedError) => {})
249251
}
250252
```
251253

@@ -310,7 +312,7 @@ const fetchUserById = createAsyncThunk(
310312
// Already fetched or in progress, don't need to re-fetch
311313
return false
312314
}
313-
}
315+
},
314316
}
315317
)
316318
```
@@ -372,7 +374,7 @@ const fetchUserById = createAsyncThunk(
372374
'users/fetchById',
373375
async (userId: string, thunkAPI) => {
374376
const response = await fetch(`https://reqres.in/api/users/${userId}`, {
375-
signal: thunkAPI.signal
377+
signal: thunkAPI.signal,
376378
})
377379
return await response.json()
378380
}
@@ -426,7 +428,7 @@ const fetchUserById = createAsyncThunk(
426428
source.cancel()
427429
})
428430
const response = await axios.get(`https://reqres.in/api/users/${userId}`, {
429-
cancelToken: source.token
431+
cancelToken: source.token,
430432
})
431433
return response.data
432434
}
@@ -459,7 +461,7 @@ const usersSlice = createSlice({
459461
entities: [],
460462
loading: 'idle',
461463
currentRequestId: undefined,
462-
error: null
464+
error: null,
463465
},
464466
reducers: {},
465467
extraReducers: {
@@ -484,15 +486,15 @@ const usersSlice = createSlice({
484486
state.error = action.error
485487
state.currentRequestId = undefined
486488
}
487-
}
488-
}
489+
},
490+
},
489491
})
490492

491493
const UsersComponent = () => {
492-
const { users, loading, error } = useSelector(state => state.users)
494+
const { users, loading, error } = useSelector((state) => state.users)
493495
const dispatch = useDispatch()
494496

495-
const fetchOneUser = async userId => {
497+
const fetchOneUser = async (userId) => {
496498
try {
497499
const resultAction = await dispatch(fetchUserById(userId))
498500
const user = unwrapResult(resultAction)
@@ -577,14 +579,14 @@ interface UsersState {
577579

578580
const initialState = {
579581
entities: {},
580-
error: null
582+
error: null,
581583
} as UsersState
582584

583585
const usersSlice = createSlice({
584586
name: 'users',
585587
initialState,
586588
reducers: {},
587-
extraReducers: builder => {
589+
extraReducers: (builder) => {
588590
// The `builder` callback form is used here because it provides correctly typed reducers from the action creators
589591
builder.addCase(updateUser.fulfilled, (state, { payload }) => {
590592
state.entities[payload.id] = payload
@@ -597,7 +599,7 @@ const usersSlice = createSlice({
597599
state.error = action.error.message
598600
}
599601
})
600-
}
602+
},
601603
})
602604

603605
export default usersSlice.reducer

docs/api/createEntityAdapter.mdx

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: createEntityAdapter
55
hide_title: true
66
---
77

8+
&nbsp;
9+
810
# `createEntityAdapter`
911

1012
## Overview
@@ -39,16 +41,16 @@ Sample usage:
3941
import {
4042
createEntityAdapter,
4143
createSlice,
42-
configureStore
44+
configureStore,
4345
} from '@reduxjs/toolkit'
4446

4547
type Book = { bookId: string; title: string }
4648

4749
const booksAdapter = createEntityAdapter<Book>({
4850
// Assume IDs are stored in a field other than `book.id`
49-
selectId: book => book.bookId,
51+
selectId: (book) => book.bookId,
5052
// Keep the "all IDs" array sorted based on book titles
51-
sortComparer: (a, b) => a.title.localeCompare(b.title)
53+
sortComparer: (a, b) => a.title.localeCompare(b.title),
5254
})
5355

5456
const booksSlice = createSlice({
@@ -61,14 +63,14 @@ const booksSlice = createSlice({
6163
booksReceived(state, action) {
6264
// Or, call them as "mutating" helpers in a case reducer
6365
booksAdapter.setAll(state, action.payload.books)
64-
}
65-
}
66+
},
67+
},
6668
})
6769

6870
const store = configureStore({
6971
reducer: {
70-
books: booksSlice.reducer
71-
}
72+
books: booksSlice.reducer,
73+
},
7274
})
7375

7476
type RootState = ReturnType<typeof store.getState>
@@ -78,7 +80,7 @@ console.log(store.getState().books)
7880

7981
// Can create a set of memoized selectors based on the location of this entity state
8082
const booksSelectors = booksAdapter.getSelectors<RootState>(
81-
state => state.books
83+
(state) => state.books
8284
)
8385

8486
// And then use the selectors to retrieve values
@@ -243,14 +245,14 @@ It accepts an optional object as an argument. The fields in that object will be
243245
const booksSlice = createSlice({
244246
name: 'books',
245247
initialState: booksAdapter.getInitialState({
246-
loading: 'idle'
248+
loading: 'idle',
247249
}),
248250
reducers: {
249251
booksLoadingStarted(state, action) {
250252
// Can update the additional state field
251253
state.loading = 'pending'
252-
}
253-
}
254+
},
255+
},
254256
})
255257
```
256258

@@ -276,12 +278,12 @@ For example, the entity state for a `Book` type might be kept in the Redux state
276278
```js
277279
const store = configureStore({
278280
reducer: {
279-
books: booksReducer
280-
}
281+
books: booksReducer,
282+
},
281283
})
282284

283285
const simpleSelectors = booksAdapter.getSelectors()
284-
const globalizedSelectors = booksAdapter.getSelectors(state => state.books)
286+
const globalizedSelectors = booksAdapter.getSelectors((state) => state.books)
285287

286288
// Need to manually pass the correct entity state object in to this selector
287289
const bookIds = simpleSelectors.selectIds(store.getState().books)
@@ -306,19 +308,19 @@ Exercising several of the CRUD methods and selectors:
306308
import {
307309
createEntityAdapter,
308310
createSlice,
309-
configureStore
311+
configureStore,
310312
} from '@reduxjs/toolkit'
311313

312314
// Since we don't provide `selectId`, it defaults to assuming `entity.id` is the right field
313315
const booksAdapter = createEntityAdapter({
314316
// Keep the "all IDs" array sorted based on book titles
315-
sortComparer: (a, b) => a.title.localeCompare(b.title)
317+
sortComparer: (a, b) => a.title.localeCompare(b.title),
316318
})
317319

318320
const booksSlice = createSlice({
319321
name: 'books',
320322
initialState: booksAdapter.getInitialState({
321-
loading: 'idle'
323+
loading: 'idle',
322324
}),
323325
reducers: {
324326
// Can pass adapter functions directly as case reducers. Because we're passing this
@@ -336,28 +338,28 @@ const booksSlice = createSlice({
336338
state.loading = 'idle'
337339
}
338340
},
339-
bookUpdated: booksAdapter.updateOne
340-
}
341+
bookUpdated: booksAdapter.updateOne,
342+
},
341343
})
342344

343345
const {
344346
bookAdded,
345347
booksLoading,
346348
booksReceived,
347-
bookUpdated
349+
bookUpdated,
348350
} = booksSlice.actions
349351

350352
const store = configureStore({
351353
reducer: {
352-
books: booksSlice.reducer
353-
}
354+
books: booksSlice.reducer,
355+
},
354356
})
355357

356358
// Check the initial state:
357359
console.log(store.getState().books)
358360
// {ids: [], entities: {}, loading: 'idle' }
359361

360-
const booksSelectors = booksAdapter.getSelectors(state => state.books)
362+
const booksSelectors = booksAdapter.getSelectors((state) => state.books)
361363

362364
store.dispatch(bookAdded({ id: 'a', title: 'First' }))
363365
console.log(store.getState().books)
@@ -371,7 +373,7 @@ console.log(store.getState().books)
371373
store.dispatch(
372374
booksReceived([
373375
{ id: 'b', title: 'Book 3' },
374-
{ id: 'c', title: 'Book 2' }
376+
{ id: 'c', title: 'Book 2' },
375377
])
376378
)
377379

0 commit comments

Comments
 (0)