Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/v1.6-integration' into f…
Browse files Browse the repository at this point in the history
…eature/cAT-meta
  • Loading branch information
phryneas committed May 31, 2021
2 parents 42aa83e + 5d59988 commit 5500116
Show file tree
Hide file tree
Showing 277 changed files with 249,551 additions and 16,259 deletions.
5 changes: 3 additions & 2 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"sandboxes": [
"vanilla",
"vanilla-ts",
"github/reduxjs/rtk-github-issues-example"
"github/reduxjs/rtk-github-issues-example",
"github/reduxjs/toolkit/examples/query/react/optimistic-updates"
],
"node": "14"
}
}
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
rules: {
'@typescript-eslint/no-unused-expressions': 'off',
'no-lone-blocks': 'off',
'no-sequences': 'off',
},
},
],
Expand Down
2 changes: 2 additions & 0 deletions docs/api/configureStore.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sidebar_label: configureStore
hide_title: true
---

 

# `configureStore`

A friendly abstraction over the standard Redux `createStore` function that adds good defaults
Expand Down
14 changes: 8 additions & 6 deletions docs/api/createAction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sidebar_label: createAction
hide_title: true
---

 

# `createAction`

A helper function for defining a Redux [action](https://redux.js.org/basics/actions) type and creator.
Expand All @@ -21,7 +23,7 @@ const INCREMENT = 'counter/increment'
function increment(amount: number) {
return {
type: INCREMENT,
payload: amount
payload: amount,
}
}

Expand Down Expand Up @@ -63,8 +65,8 @@ const addTodo = createAction('todos/add', function prepare(text: string) {
payload: {
text,
id: nanoid(),
createdAt: new Date().toISOString()
}
createdAt: new Date().toISOString(),
},
}
})

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

const counterReducer = createReducer(0, builder => {
const counterReducer = createReducer(0, (builder) => {
builder.addCase(increment, (state, action) => state + action.payload)
builder.addCase(decrement, (state, action) => state - action.payload)
})
Expand Down Expand Up @@ -133,7 +135,7 @@ const counterReducer = createReducer(0, {
[increment]: (state, action) => state + action.payload,

// You would need to use the action type explicitly instead.
[INCREMENT]: (state, action) => state + action.payload
[INCREMENT]: (state, action) => state + action.payload,
})
```
Expand Down Expand Up @@ -178,7 +180,7 @@ const increment = createAction<number>('INCREMENT')
export const epic = (actions$: Observable<Action>) =>
actions$.pipe(
filter(increment.match),
map(action => {
map((action) => {
// action.payload can be safely used as number here (and will also be correctly inferred by TypeScript)
// ...
})
Expand Down
44 changes: 23 additions & 21 deletions docs/api/createAsyncThunk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sidebar_label: createAsyncThunk
hide_title: true
---

&nbsp;

# `createAsyncThunk`

## Overview
Expand Down Expand Up @@ -42,8 +44,8 @@ const usersSlice = createSlice({
[fetchUserById.fulfilled]: (state, action) => {
// Add user to the state array
state.entities.push(action.payload)
}
}
},
},
})

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

const reducer2 = createReducer(initialState, builder => {
const reducer2 = createReducer(initialState, (builder) => {
builder.addCase(fetchUserById.fulfilled, (state, action) => {})
})

Expand All @@ -208,17 +210,17 @@ const reducer3 = createSlice({
initialState,
reducers: {},
extraReducers: {
[fetchUserById.fulfilled]: (state, action) => {}
}
[fetchUserById.fulfilled]: (state, action) => {},
},
})

const reducer4 = createSlice({
name: 'users',
initialState,
reducers: {},
extraReducers: builder => {
extraReducers: (builder) => {
builder.addCase(fetchUserById.fulfilled, (state, action) => {})
}
},
})
```

Expand Down Expand Up @@ -247,8 +249,8 @@ import { unwrapResult } from '@reduxjs/toolkit'
const onClick = () => {
dispatch(fetchUserById(userId))
.then(unwrapResult)
.then(originalPromiseResult => {})
.catch(rejectedValueOrSerializedError => {})
.then((originalPromiseResult) => {})
.catch((rejectedValueOrSerializedError) => {})
}
```

Expand Down Expand Up @@ -313,7 +315,7 @@ const fetchUserById = createAsyncThunk(
// Already fetched or in progress, don't need to re-fetch
return false
}
}
},
}
)
```
Expand Down Expand Up @@ -375,7 +377,7 @@ const fetchUserById = createAsyncThunk(
'users/fetchById',
async (userId: string, thunkAPI) => {
const response = await fetch(`https://reqres.in/api/users/${userId}`, {
signal: thunkAPI.signal
signal: thunkAPI.signal,
})
return await response.json()
}
Expand Down Expand Up @@ -429,7 +431,7 @@ const fetchUserById = createAsyncThunk(
source.cancel()
})
const response = await axios.get(`https://reqres.in/api/users/${userId}`, {
cancelToken: source.token
cancelToken: source.token,
})
return response.data
}
Expand Down Expand Up @@ -462,7 +464,7 @@ const usersSlice = createSlice({
entities: [],
loading: 'idle',
currentRequestId: undefined,
error: null
error: null,
},
reducers: {},
extraReducers: {
Expand All @@ -487,15 +489,15 @@ const usersSlice = createSlice({
state.error = action.error
state.currentRequestId = undefined
}
}
}
},
},
})

const UsersComponent = () => {
const { users, loading, error } = useSelector(state => state.users)
const { users, loading, error } = useSelector((state) => state.users)
const dispatch = useDispatch()

const fetchOneUser = async userId => {
const fetchOneUser = async (userId) => {
try {
const resultAction = await dispatch(fetchUserById(userId))
const user = unwrapResult(resultAction)
Expand Down Expand Up @@ -580,14 +582,14 @@ interface UsersState {

const initialState = {
entities: {},
error: null
error: null,
} as UsersState

const usersSlice = createSlice({
name: 'users',
initialState,
reducers: {},
extraReducers: builder => {
extraReducers: (builder) => {
// The `builder` callback form is used here because it provides correctly typed reducers from the action creators
builder.addCase(updateUser.fulfilled, (state, { payload }) => {
state.entities[payload.id] = payload
Expand All @@ -600,7 +602,7 @@ const usersSlice = createSlice({
state.error = action.error.message
}
})
}
},
})

export default usersSlice.reducer
Expand Down
50 changes: 26 additions & 24 deletions docs/api/createEntityAdapter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sidebar_label: createEntityAdapter
hide_title: true
---

&nbsp;

# `createEntityAdapter`

## Overview
Expand Down Expand Up @@ -39,16 +41,16 @@ Sample usage:
import {
createEntityAdapter,
createSlice,
configureStore
configureStore,
} from '@reduxjs/toolkit'

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

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

const booksSlice = createSlice({
Expand All @@ -61,14 +63,14 @@ const booksSlice = createSlice({
booksReceived(state, action) {
// Or, call them as "mutating" helpers in a case reducer
booksAdapter.setAll(state, action.payload.books)
}
}
},
},
})

const store = configureStore({
reducer: {
books: booksSlice.reducer
}
books: booksSlice.reducer,
},
})

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

// Can create a set of memoized selectors based on the location of this entity state
const booksSelectors = booksAdapter.getSelectors<RootState>(
state => state.books
(state) => state.books
)

// And then use the selectors to retrieve values
Expand Down Expand Up @@ -243,14 +245,14 @@ It accepts an optional object as an argument. The fields in that object will be
const booksSlice = createSlice({
name: 'books',
initialState: booksAdapter.getInitialState({
loading: 'idle'
loading: 'idle',
}),
reducers: {
booksLoadingStarted(state, action) {
// Can update the additional state field
state.loading = 'pending'
}
}
},
},
})
```

Expand All @@ -276,12 +278,12 @@ For example, the entity state for a `Book` type might be kept in the Redux state
```js
const store = configureStore({
reducer: {
books: booksReducer
}
books: booksReducer,
},
})

const simpleSelectors = booksAdapter.getSelectors()
const globalizedSelectors = booksAdapter.getSelectors(state => state.books)
const globalizedSelectors = booksAdapter.getSelectors((state) => state.books)

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

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

const booksSlice = createSlice({
name: 'books',
initialState: booksAdapter.getInitialState({
loading: 'idle'
loading: 'idle',
}),
reducers: {
// Can pass adapter functions directly as case reducers. Because we're passing this
Expand All @@ -336,28 +338,28 @@ const booksSlice = createSlice({
state.loading = 'idle'
}
},
bookUpdated: booksAdapter.updateOne
}
bookUpdated: booksAdapter.updateOne,
},
})

const {
bookAdded,
booksLoading,
booksReceived,
bookUpdated
bookUpdated,
} = booksSlice.actions

const store = configureStore({
reducer: {
books: booksSlice.reducer
}
books: booksSlice.reducer,
},
})

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

const booksSelectors = booksAdapter.getSelectors(state => state.books)
const booksSelectors = booksAdapter.getSelectors((state) => state.books)

store.dispatch(bookAdded({ id: 'a', title: 'First' }))
console.log(store.getState().books)
Expand All @@ -371,7 +373,7 @@ console.log(store.getState().books)
store.dispatch(
booksReceived([
{ id: 'b', title: 'Book 3' },
{ id: 'c', title: 'Book 2' }
{ id: 'c', title: 'Book 2' },
])
)

Expand Down
Loading

0 comments on commit 5500116

Please sign in to comment.