Skip to content

Commit b781f5c

Browse files
authored
Merge pull request #1230 from PrinceRajRoy/docs-update
2 parents cb3b543 + 0cf9004 commit b781f5c

File tree

5 files changed

+165
-54
lines changed

5 files changed

+165
-54
lines changed

docs/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"checkJs": true,
1717
"alwaysStrict": false,
1818
"baseUrl": "..",
19+
"jsx": "preserve",
1920
"paths": {
2021
"@reduxjs/toolkit": ["packages/toolkit/src/index.ts"],
2122
"@reduxjs/toolkit/query": ["packages/toolkit/src/query/index.ts"],

docs/tutorials/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ See these linked tutorials to learn how to use Redux Toolkit effectively.
2525

2626
## Redux Toolkit Quick Starts
2727

28-
The [**Redux Toolkit Quick Start tutorial**](./quick-start.md) briefly shows how to add and use Redux Toolkit in a React application.
28+
The [**Redux Toolkit Quick Start tutorial**](./quick-start.mdx) briefly shows how to add and use Redux Toolkit in a React application.
2929

3030
**If you just want the fastest way to get a basic example running, read the Quick Start tutorial.**
3131

docs/tutorials/quick-start.md renamed to docs/tutorials/quick-start.mdx

Lines changed: 94 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ npm install @reduxjs/toolkit react-redux
4949

5050
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:
5151

52-
```js title="app/store.js"
52+
```ts title="app/store.js"
5353
import { configureStore } from '@reduxjs/toolkit'
5454

55-
export default configureStore({
55+
export const store = configureStore({
5656
reducer: {},
5757
})
58+
59+
// Infer the `RootState` and `AppDispatch` types from the store itself
60+
export type RootState = ReturnType<typeof store.getState>
61+
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
62+
export type AppDispatch = typeof store.dispatch
5863
```
5964
6065
This creates a Redux store, and also automatically configure the Redux DevTools extension so that you can inspect the store while developing.
@@ -63,13 +68,27 @@ This creates a Redux store, and also automatically configure the Redux DevTools
6368
6469
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:
6570
66-
```js title="index.js"
71+
```ts title="index.js"
72+
// file: App.tsx noEmit
73+
import React from 'react'
74+
export default function App() {
75+
return <div>...</div>
76+
}
77+
78+
// file: app/store.ts noEmit
79+
import { configureStore } from '@reduxjs/toolkit'
80+
81+
export const store = configureStore({
82+
reducer: {},
83+
})
84+
85+
// file: index.tsx
6786
import React from 'react'
6887
import ReactDOM from 'react-dom'
6988
import './index.css'
7089
import App from './App'
7190
// highlight-start
72-
import store from './app/store'
91+
import { store } from './app/store'
7392
import { Provider } from 'react-redux'
7493
// highlight-end
7594

@@ -90,14 +109,20 @@ Creating a slice requires a string name to identify the slice, an initial state
90109

91110
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).
92111

93-
```js title="features/counter/counterSlice.js"
94-
import { createSlice } from '@reduxjs/toolkit'
112+
```ts title="features/counter/counterSlice.js"
113+
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
114+
115+
interface CounterState {
116+
value: number
117+
}
118+
119+
const initialState: CounterState = {
120+
value: 0,
121+
}
95122

96123
export const counterSlice = createSlice({
97124
name: 'counter',
98-
initialState: {
99-
value: 0,
100-
},
125+
initialState,
101126
reducers: {
102127
increment: (state) => {
103128
// Redux Toolkit allows us to write "mutating" logic in reducers. It
@@ -109,7 +134,7 @@ export const counterSlice = createSlice({
109134
decrement: (state) => {
110135
state.value -= 1
111136
},
112-
incrementByAmount: (state, action) => {
137+
incrementByAmount: (state, action: PayloadAction<number>) => {
113138
state.value += action.payload
114139
},
115140
},
@@ -125,7 +150,19 @@ export default counterSlice.reducer
125150

126151
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.
127152

128-
```js title="app/store.js"
153+
```ts title="app/store.js"
154+
// file: features/counter/counterSlice.ts noEmit
155+
import { createSlice } from '@reduxjs/toolkit'
156+
157+
const counterSlice = createSlice({
158+
name: 'counter',
159+
initialState: {},
160+
reducers: {},
161+
})
162+
163+
export default counterSlice.reducer
164+
165+
// file: app/store.ts
129166
import { configureStore } from '@reduxjs/toolkit'
130167
// highlight-next-line
131168
import counterReducer from '../features/counter/counterSlice'
@@ -142,13 +179,56 @@ export default configureStore({
142179

143180
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>`.
144181

145-
```jsx title="features/counter/Counter.js"
182+
```ts title="features/counter/Counter.js"
183+
// file: features/counter/counterSlice.ts noEmit
184+
import { createSlice } from '@reduxjs/toolkit'
185+
const counterSlice = createSlice({
186+
name: 'counter',
187+
initialState: {
188+
value: 0,
189+
},
190+
reducers: {
191+
increment: (state) => {},
192+
decrement: (state) => {},
193+
},
194+
})
195+
196+
export const { increment, decrement } = counterSlice.actions
197+
export default counterSlice.reducer
198+
199+
// file: app/store.ts noEmit
200+
import { configureStore } from '@reduxjs/toolkit'
201+
import counterReducer from '../features/counter/counterSlice'
202+
export const store = configureStore({
203+
reducer: {
204+
counter: counterReducer,
205+
},
206+
})
207+
export type RootState = ReturnType<typeof store.getState>
208+
209+
// file: index.tsx noEmit
210+
import React from 'react'
211+
import ReactDOM from 'react-dom'
212+
import { Counter } from './features/counter/Counter'
213+
import { store } from './app/store'
214+
import { Provider } from 'react-redux'
215+
216+
ReactDOM.render(
217+
// highlight-next-line
218+
<Provider store={store}>
219+
<Counter />
220+
</Provider>,
221+
document.getElementById('root')
222+
)
223+
224+
// file: features/counter/Counter.tsx
146225
import React from 'react'
226+
import { RootState } from '../../app/store'
147227
import { useSelector, useDispatch } from 'react-redux'
148228
import { decrement, increment } from './counterSlice'
149229

150230
export function Counter() {
151-
const count = useSelector((state) => state.counter.value)
231+
const count = useSelector((state: RootState) => state.counter.value)
152232
const dispatch = useDispatch()
153233

154234
return (
@@ -208,7 +288,7 @@ The counter example app shown here is also the
208288
Here's the complete counter application as a running CodeSandbox:
209289

210290
<iframe
211-
class="codesandbox"
291+
className="codesandbox"
212292
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"
213293
title="redux-essentials-example"
214294
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"

docs/tutorials/rtk-query.mdx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,27 @@ setupListeners(store.dispatch)
128128

129129
If you haven't already done so, follow the standard pattern for providing the Redux store to the rest of your React application component tree:
130130

131-
```tsx title="src/index.tsx"
131+
```ts title="src/index.tsx"
132+
// file: App.tsx noEmit
133+
import React from 'react'
134+
export default function App() {
135+
return <div>...</div>
136+
}
137+
138+
// file: app/store.ts noEmit
139+
import { configureStore } from '@reduxjs/toolkit'
140+
141+
export default configureStore({
142+
reducer: {},
143+
})
144+
145+
// file: index.tsx
132146
import * as React from 'react'
133147
import { render } from 'react-dom'
134148
import { Provider } from 'react-redux'
135149

136150
import App from './App'
137-
import { store } from './store'
151+
import store from './app/store'
138152

139153
const rootElement = document.getElementById('root')
140154
render(
@@ -149,7 +163,23 @@ render(
149163

150164
Once a service has been defined, you can import the hooks to make a request.
151165

152-
```tsx title="src/App.tsx" no-transpile
166+
```ts title="src/App.tsx"
167+
// file: services/pokemon.ts noEmit
168+
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
169+
170+
export const pokemonApi = createApi({
171+
reducerPath: 'pokemonApi',
172+
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
173+
endpoints: (builder) => ({
174+
getPokemonByName: builder.query({
175+
query: (name: string) => `pokemon/${name}`,
176+
}),
177+
}),
178+
})
179+
180+
export const { useGetPokemonByNameQuery } = pokemonApi
181+
182+
// file: App.tsx
153183
import * as React from 'react'
154184
// highlight-next-line
155185
import { useGetPokemonByNameQuery } from './services/pokemon'

website/package.json

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
{
2-
"name": "website",
3-
"scripts": {
4-
"start": "docusaurus start",
5-
"build": "docusaurus build",
6-
"netlify-build": "yarn install && yarn build",
7-
"swizzle": "docusaurus swizzle",
8-
"publish-gh-pages": "docusaurus deploy",
9-
"deploy": "docusaurus deploy"
10-
},
11-
"dependencies": {
12-
"@docusaurus/core": "2.0.0-beta.1",
13-
"@docusaurus/preset-classic": "2.0.0-beta.1",
14-
"classnames": "^2.2.6",
15-
"react": "17.0.2",
16-
"react-dom": "17.0.2",
17-
"react-lite-youtube-embed": "^2.0.3",
18-
"remark-typescript-tools": "^1.0.7",
19-
"typescript": "~4.2.4"
20-
},
21-
"browserslist": {
22-
"production": [
23-
">0.2%",
24-
"not dead",
25-
"not op_mini all"
26-
],
27-
"development": [
28-
"last 1 chrome version",
29-
"last 1 firefox version",
30-
"last 1 safari version"
31-
]
32-
},
33-
"devDependencies": {
34-
"netlify-plugin-cache": "^1.0.3"
35-
}
36-
}
1+
{
2+
"name": "website",
3+
"scripts": {
4+
"start": "docusaurus start",
5+
"build": "docusaurus build",
6+
"netlify-build": "yarn install && yarn build",
7+
"swizzle": "docusaurus swizzle",
8+
"publish-gh-pages": "docusaurus deploy",
9+
"deploy": "docusaurus deploy"
10+
},
11+
"dependencies": {
12+
"@docusaurus/core": "2.0.0-beta.1",
13+
"@docusaurus/preset-classic": "2.0.0-beta.1",
14+
"classnames": "^2.2.6",
15+
"react": "17.0.2",
16+
"react-dom": "17.0.2",
17+
"react-lite-youtube-embed": "^2.0.3",
18+
"remark-typescript-tools": "^1.0.8",
19+
"typescript": "~4.2.4"
20+
},
21+
"browserslist": {
22+
"production": [
23+
">0.2%",
24+
"not dead",
25+
"not op_mini all"
26+
],
27+
"development": [
28+
"last 1 chrome version",
29+
"last 1 firefox version",
30+
"last 1 safari version"
31+
]
32+
},
33+
"devDependencies": {
34+
"netlify-plugin-cache": "^1.0.3"
35+
}
36+
}

0 commit comments

Comments
 (0)