Skip to content

Commit 6a6ab8e

Browse files
committed
fix(core): preserve RN initial page params
1 parent 510ef89 commit 6a6ab8e

4 files changed

Lines changed: 17 additions & 16 deletions

File tree

.agents/skills/mpx2rn/references/rn-script-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
| `pageLifetimes.show` | 组件 | 所在页面展示或重新获得焦点时触发,与页面 `onShow` 时机对齐。 |
151151
| `pageLifetimes.hide` | 组件 | 所在页面隐藏或失焦时触发,与页面 `onHide` 时机对齐。 |
152152
| `pageLifetimes.resize` | 组件 | 所在页面可视区域尺寸变化时触发,与页面 `onResize` 时机对齐。 |
153-
| `onLoad` | 页面 | 页面创建后调用,**两个参数** `(rawQuery, decodedQuery)`|
153+
| `onLoad` | 页面 | 页面创建后调用,**两个参数** `(rawQuery, decodedQuery)`;冷启动首次首页会保留 `parseAppProps` 返回的原始 `initialParams`,并与路由参数合并|
154154
| `created` | 组件 | 组件实例刚创建,RN 由 `MpxProxy` 在实例建立阶段调度,此时不宜依赖完整视图。 |
155155
| `attached` | 组件 | 组件进入节点树,RN 对齐为挂载流程中的对应阶段,详见 `docs-vitepress/guide/basic/lifecycle.md` 映射表。 |
156156
| `ready` | 组件 | 组件布局完成、可与视图交互,RN 对应 React 挂载后的就绪时机,与页面 `onReady` 同属一套内置映射。 |
@@ -753,7 +753,7 @@ Mpx.config.rnConfig = {
753753
| 配置项 | 说明 |
754754
| --- | --- |
755755
| `projectName` | 由构建注入到 RN 入口,与 `AppRegistry.registerComponent` 相关(偏构建侧)。 |
756-
| `parseAppProps` | `(props) => { initialRouteName?, initialParams? }`,解析外层传入 App 根组件的初始路由。 |
756+
| `parseAppProps` | `(props) => { initialRouteName?, initialParams? }`,解析外层传入 App 根组件的初始路由`initialParams` 会原样传给冷启动首次首页的 `onLoad`,并在初始路由实例存续期间作为应用 `onLaunch` / `onShow``query`|
757757
| `onStateChange` | 导航 state 变化时回调。 |
758758
| `disablePageTransition` |`true` 时禁用 RN 页面转场动画,框架内部映射为 `animation: "none"`|
759759
| `disableAppStateListener` |`true` 时不注册 `AppState` 监听(避免与宿主 App 重复)。 |

docs-vitepress/guide/rn/application-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ createComponent({
417417
用于获取初始路由配置的函数,参数为 RN 根组件接收到的参数
418418

419419
- initialRouteName: 首页路径,例如 pages/index
420-
- initialParams: 将作为 首页onLoad 与 应用onLaunch 的参数,例如 \{ a: 1 \}
420+
- initialParams: 冷启动时将原样作为首次首页 `onLoad` 的参数,并在初始路由实例存续期间作为应用 `onLaunch` / `onShow``query`,例如 \{ a: 1 \}。首次首页同时存在路由参数时会进行合并,路由参数优先
421421

422422
在需要将 RN 应用嵌入到现有的 NA 应用中时,NA 可能会向 RN 的根组件传递 props,此时可在 parseAppProps 中接受 props 并进行处理和透传到页面
423423

packages/core/src/platform/createApp.ios.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default function createApp (options) {
5252
const pagesMap = currentInject.pagesMap || {}
5353
const firstPage = currentInject.firstPage
5454
const Stack = createNativeStackNavigator()
55-
const getPageScreens = (initialRouteName, initialParams) => {
55+
const getPageScreens = () => {
5656
return Object.entries(pagesMap).map(([key, item]) => {
5757
const pageConfig = Object.assign({}, global.__mpxPageConfig, global.__mpxPageConfigsMap[key])
5858
const headerLayout = ({ navigation, children }) => {
@@ -72,14 +72,6 @@ export default function createApp (options) {
7272
const getComponent = () => {
7373
return item.displayName ? item : callWithErrorHandling(item, null, 'require page script')
7474
}
75-
if (key === initialRouteName) {
76-
return createElement(Stack.Screen, {
77-
name: key,
78-
getComponent,
79-
initialParams,
80-
layout: headerLayout
81-
})
82-
}
8375
return createElement(Stack.Screen, {
8476
name: key,
8577
getComponent,
@@ -120,7 +112,7 @@ export default function createApp (options) {
120112
const current = state.routes[state.index]
121113
options = {
122114
path: current.name,
123-
query: current.params,
115+
query: current.key === global.__mpxInitialRouteKey ? global.__mpxInitialRunParams : current.params,
124116
scene: 0,
125117
shareTicket: '',
126118
referrerInfo: {}
@@ -178,9 +170,10 @@ export default function createApp (options) {
178170
const state = navigation.getState()
179171
Mpx.config.rnConfig.onStateChange?.(state)
180172
const current = state.routes[state.index]
173+
global.__mpxInitialRouteKey = current.key
181174
const options = {
182175
path: current.name,
183-
query: current.params,
176+
query: global.__mpxInitialRunParams,
184177
scene: 0,
185178
shareTicket: '',
186179
referrerInfo: {},
@@ -211,6 +204,10 @@ export default function createApp (options) {
211204
}, [])
212205

213206
const { initialRouteName, initialParams } = initialRouteRef.current
207+
if (!global.__mpxAppHotLaunched) {
208+
global.__mpxInitialRouteKey = null
209+
global.__mpxInitialRunParams = initialParams
210+
}
214211
const navScreenOpts = {
215212
headerShown: false
216213
}
@@ -230,7 +227,7 @@ export default function createApp (options) {
230227
initialRouteName,
231228
screenOptions: navScreenOpts
232229
},
233-
...getPageScreens(initialRouteName, initialParams)
230+
...getPageScreens()
234231
)
235232
)
236233
)

packages/core/src/platform/patch/getDefaultOptions.ios.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,16 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps
296296
})
297297
}
298298

299+
let loadParams
299300
if (type === 'page') {
300301
const props = propsRef.current
301302
instance.route = props.route.name
302303
global.__mpxPagesMap = global.__mpxPagesMap || {}
303304
global.__mpxPagesMap[props.route.key] = [instance, props.navigation]
304305
setFocusedNavigation(props.navigation)
306+
if (!global.__mpxAppHotLaunched && global.__mpxInitialRunParams) {
307+
loadParams = Object.assign({}, global.__mpxInitialRunParams)
308+
}
305309
set(global.__mpxPageSizeCountMap, pageId, global.__mpxSizeCount)
306310
// App onLaunch 在 Page created 之前执行
307311
if (!global.__mpxAppHotLaunched && global.__mpxAppOnLaunch) {
@@ -315,7 +319,7 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps
315319
if (type === 'page') {
316320
const props = propsRef.current
317321
const decodedQuery = {}
318-
const rawQuery = props.route.params || {}
322+
const rawQuery = Object.assign({}, loadParams, props.route.params || {})
319323
if (isObject(rawQuery)) {
320324
for (const key in rawQuery) {
321325
try {

0 commit comments

Comments
 (0)