Skip to content

Commit bdd0127

Browse files
committed
Migrate docs to React 19
1 parent 8adf370 commit bdd0127

File tree

4 files changed

+70
-34
lines changed

4 files changed

+70
-34
lines changed

docs/tutorials/quick-start.mdx

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,30 @@ export const store = configureStore({
8484

8585
// file: index.tsx
8686
import React from 'react'
87-
import ReactDOM from 'react-dom'
87+
import { createRoot } from 'react-dom/client'
8888
import './index.css'
8989
import App from './App'
9090
// highlight-start
9191
import { store } from './app/store'
9292
import { Provider } from 'react-redux'
9393
// highlight-end
9494

95-
ReactDOM.render(
96-
// highlight-next-line
97-
<Provider store={store}>
98-
<App />
99-
</Provider>,
100-
document.getElementById('root')
101-
)
95+
const container = document.getElementById('root')
96+
97+
if (container) {
98+
const root = createRoot(container)
99+
100+
root.render(
101+
// highlight-next-line
102+
<Provider store={store}>
103+
<App />
104+
</Provider>,
105+
)
106+
} else {
107+
throw new Error(
108+
"Root element with ID 'root' was not found in the document. Ensure there is a corresponding HTML element with the ID 'root' in your HTML file.",
109+
)
110+
}
102111
```
103112

104113
### Create a Redux State Slice
@@ -214,18 +223,27 @@ export type RootState = ReturnType<typeof store.getState>
214223

215224
// file: index.tsx noEmit
216225
import React from 'react'
217-
import ReactDOM from 'react-dom'
226+
import { createRoot } from 'react-dom/client'
218227
import { Counter } from './features/counter/Counter'
219228
import { store } from './app/store'
220229
import { Provider } from 'react-redux'
221230

222-
ReactDOM.render(
223-
// highlight-next-line
224-
<Provider store={store}>
225-
<Counter />
226-
</Provider>,
227-
document.getElementById('root')
228-
)
231+
const container = document.getElementById('root')
232+
233+
if (container) {
234+
const root = createRoot(container)
235+
236+
root.render(
237+
// highlight-next-line
238+
<Provider store={store}>
239+
<Counter />
240+
</Provider>,
241+
)
242+
} else {
243+
throw new Error(
244+
"Root element with ID 'root' was not found in the document. Ensure there is a corresponding HTML element with the ID 'root' in your HTML file.",
245+
)
246+
}
229247

230248
// file: features/counter/Counter.tsx
231249
import React from 'react'

docs/tutorials/rtk-query.mdx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,28 @@ export const store = configureStore({
149149
})
150150

151151
// file: index.tsx
152-
import * as React from 'react'
153-
import { render } from 'react-dom'
152+
import React from 'react'
153+
import { createRoot } from 'react-dom/client'
154154
import { Provider } from 'react-redux'
155155

156156
import App from './App'
157157
import { store } from './store'
158158

159-
const rootElement = document.getElementById('root')
160-
render(
161-
<Provider store={store}>
162-
<App />
163-
</Provider>,
164-
rootElement
165-
)
159+
const container = document.getElementById('root')
160+
161+
if (container) {
162+
const root = createRoot(container)
163+
164+
root.render(
165+
<Provider store={store}>
166+
<App />
167+
</Provider>,
168+
)
169+
} else {
170+
throw new Error(
171+
"Root element with ID 'root' was not found in the document. Ensure there is a corresponding HTML element with the ID 'root' in your HTML file.",
172+
)
173+
}
166174
```
167175

168176
## Use the query in a component

docs/usage/nextjs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export default function StoreProvider({
181181
children: React.ReactNode
182182
}) {
183183
// highlight-start
184-
const storeRef = useRef<AppStore>()
184+
const storeRef = useRef<AppStore>(undefined)
185185
if (!storeRef.current) {
186186
// Create the store instance the first time this renders
187187
storeRef.current = makeStore()

docs/usage/usage-guide.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ configureStore({
10411041
If using Redux-Persist, you should specifically ignore all the action types it dispatches:
10421042

10431043
```jsx
1044+
import { createRoot } from 'react-dom/client'
10441045
import { configureStore } from '@reduxjs/toolkit'
10451046
import {
10461047
persistStore,
@@ -1078,14 +1079,23 @@ const store = configureStore({
10781079

10791080
let persistor = persistStore(store)
10801081

1081-
ReactDOM.render(
1082-
<Provider store={store}>
1083-
<PersistGate loading={null} persistor={persistor}>
1084-
<App />
1085-
</PersistGate>
1086-
</Provider>,
1087-
document.getElementById('root'),
1088-
)
1082+
const container = document.getElementById('root')
1083+
1084+
if (container) {
1085+
const root = createRoot(container)
1086+
1087+
root.render(
1088+
<Provider store={store}>
1089+
<PersistGate loading={null} persistor={persistor}>
1090+
<App />
1091+
</PersistGate>
1092+
</Provider>,
1093+
)
1094+
} else {
1095+
throw new Error(
1096+
"Root element with ID 'root' was not found in the document. Ensure there is a corresponding HTML element with the ID 'root' in your HTML file.",
1097+
)
1098+
}
10891099
```
10901100

10911101
Additionally, you can purge any persisted state by adding an extra reducer to the specific slice that you would like to clear when calling persistor.purge(). This is especially helpful when you are looking to clear persisted state on a dispatched logout action.

0 commit comments

Comments
 (0)