Skip to content

Commit c76f35b

Browse files
authored
Merge pull request #2383 from inertiajs/cleanup-router-init
Remove `setSwapComponent` method and cleanup after PR #2379
2 parents f96e347 + 6370383 commit c76f35b

4 files changed

Lines changed: 18 additions & 22 deletions

File tree

packages/core/src/page.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class CurrentPage {
3434
return this
3535
}
3636

37-
public setSwapComponent(swapComponent: PageHandler): void {
38-
this.swapComponent = swapComponent
39-
}
40-
4137
public set(
4238
page: Page,
4339
{

packages/core/src/router.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
GlobalEventResult,
1919
InFlightPrefetch,
2020
Page,
21-
PageHandler,
2221
PendingVisit,
2322
PendingVisitOptions,
2423
PollOptions,
@@ -67,10 +66,6 @@ export class Router {
6766
})
6867
}
6968

70-
public setSwapComponent(swapComponent: PageHandler): void {
71-
currentPage.setSwapComponent(swapComponent)
72-
}
73-
7469
public get<T extends RequestPayload = RequestPayload>(
7570
url: URL | string,
7671
data: T = {} as T,

packages/react/src/App.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { createHeadManager, router } from '@inertiajs/core'
1+
import { createHeadManager, PageHandler, router } from '@inertiajs/core'
22
import { createElement, useEffect, useMemo, useState } from 'react'
33
import HeadContext from './HeadContext'
44
import PageContext from './PageContext'
55

6-
let isRouterInitialized = false
76
let currentIsInitialPage = true
7+
let routerIsInitialized = false
8+
let swapComponent: PageHandler = async () => {
9+
// Dummy function so we can init the router outside of the useEffect hook. This is
10+
// needed so `router.reload()` works right away (on mount) in any of the user's
11+
// components. We swap in the real function in the useEffect hook below.
12+
currentIsInitialPage = false
13+
}
814

915
export default function App({
1016
children,
@@ -28,20 +34,21 @@ export default function App({
2834
)
2935
}, [])
3036

31-
if (!isRouterInitialized) {
37+
if (!routerIsInitialized) {
3238
router.init({
3339
initialPage,
3440
resolveComponent,
35-
swapComponent: async () => {
36-
currentIsInitialPage = false
37-
},
41+
swapComponent: async (args) => swapComponent(args),
3842
})
39-
isRouterInitialized = true
43+
44+
routerIsInitialized = true
4045
}
4146

4247
useEffect(() => {
43-
router.setSwapComponent(async ({ component, page, preserveState }) => {
48+
swapComponent = async ({ component, page, preserveState }) => {
4449
if (currentIsInitialPage) {
50+
// We block setting the current page on the initial page to
51+
// prevent the initial page from being re-rendered again.
4552
currentIsInitialPage = false
4653
return
4754
}
@@ -51,7 +58,7 @@ export default function App({
5158
page,
5259
key: preserveState ? current.key : Date.now(),
5360
}))
54-
})
61+
}
5562

5663
router.on('navigate', () => headManager.forceUpdate())
5764
}, [])

packages/react/test-app/Pages/Visits/ReloadOnMount.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { useEffect } from 'react'
33

44
export default (props) => {
55
useEffect(() => {
6-
setTimeout(() => {
7-
router.reload({ only: ['name'] })
8-
})
9-
})
6+
router.reload({ only: ['name'] })
7+
}, [])
108

119
return <div>Name is {props.name}</div>
1210
}

0 commit comments

Comments
 (0)