【发布时间】:2021-12-27 15:33:11
【问题描述】:
我有一堆屏幕,用于创建将在最后显示的对象。
在通过屏幕传递参数时,直到最后一个都没有问题。
当您在最后一个屏幕上确认导航到仪表板(列出所有对象)的功能触发时,它会去它需要去的地方,但在检索构造函数中的参数时,它们会返回为 @ 987654321@,即使通过调试器我也可以清楚地看到该道具已成功传递给导航功能。
下面这段代码从后端调用api来检索当前的user,如果成功则触发openDashboardHouses函数:
new UsersApi(context).apiUsersCurrentMeGet().then(resp => {
console.log(resp);
// passing context - user - house object
openDashboardHouses(context, resp, context.state.house);
}).catch(error => {
console.log(JSON.stringify(error))
AlertHelper.showSimpleErrorAlert();
});
openDashboardHouses 函数只是要导航的一行代码:
export function openDashboardHouses(context: Component<NavigationProps, any>, user: User, house: House = null) {
context.props.navigation.navigate(dashboardHousesRoute, {user: user, house: house}); // During debugging the values on this line are correctly filled
}
然后在下一个屏幕上它应该在构造函数中获取道具并将它们保存在状态中:
constructor(props) {
super(props);
setHeaderTitleLocalized(props, 'screens.dashboard.title');
this.state = {
loading: false,
user: getUserFromNavigationProps(this),
houses: [],
resources: [],
created_house: getHouseFromNavigationProps(this),
};
}
如你所见,我有两个辅助函数,它们都不返回任何内容
export function getUserFromNavigationProps(context: Component<NavigationProps, any>) {
return context.props.navigation.getParam(USER_KEY); // USER_KEY = 'user'
}
export function getHouseFromNavigationProps(context: Component<NavigationProps, any>): House {
return context.props.navigation.getParam(HOUSE_KEY); // HOUSE_KEY = 'house'
}
知道发生了什么吗?
附加信息:该房屋道具是在第一个屏幕中创建的,并且在所有屏幕中都以相同的方式传递。只是最后一个不想合作
依赖关系:
反应原生:
├─ react-native@0.62.2
│ ├─ @sentry/react-native@^1.0.0
反应导航:
├─ @react-navigation/core@3.5.0
├─ @react-navigation/native@3.6.2
├─ @react-navigation/web@1.0.0-alpha.9
├─ react-navigation-drawer@1.4.0
├─ react-navigation-stack@1.5.3
├─ react-navigation-tabs@1.2.0
├─ react-navigation@3.12.1
│ ├─ @react-navigation/core@~3.5.0
│ ├─ @react-navigation/native@~3.6.1
│ ├─ react-navigation-drawer@~1.4.0
│ ├─ react-navigation-stack@~1.5.0
│ └─ react-navigation-tabs@~1.2.0
【问题讨论】:
-
你运行的是哪个版本的 react-navigation?
-
@aetheode @react-navigation/core@3.5.0 -- ├─ @react-navigation/native@3.6.2 -- ├─ @react-navigation/web@1.0.0-alpha .9 -- ├─ react-navigation-drawer@1.4.0 -- ├─ react-navigation-stack@1.5.3 -- ├─ react-navigation-tabs@1.2.0 -- ├─ react-navigation@3.12 .1 -- │ ├─ @react-navigation/core@~3.5.0 -- │ ├─ @react-navigation/native@~3.6.1 -- │ ├─ react-navigation-drawer@~1.4.0 - - │ ├─ react-navigation-stack@~1.5.0 -- │ └─ react-navigation-tabs@~1.2.0 --
-
啊,看来整体版本是 react-native 4.x
-
@aetheode 是兼容性问题吗?因为除了这里,其他地方一切正常
-
我唯一能想到的是,您要导航到的屏幕可能不在您要导航的屏幕的上下文中,所以它有点迷路了。抱歉,自从我使用这个早期版本的 react-navigation 以来已经有一段时间了。您是否可以探索将您的库升级到最新版本,因为引入了不同的上下文提供程序,使这样的数据管理变得更加容易,imo
标签: typescript react-native react-navigation