javascript - 在 React native 中向下传递参数的问题
问题描述
我很难使用 React StackNavigator,并将参数向下传递到屏幕。我只是在学习 React Native 如何工作的阶段,所以也许这不是最佳实践,所以如果有更好的方法,我愿意接受其他建议。
function SetupsStack(props) {
console.log(props.route.params.Setup,"route is") // This has what I want
return (
<Stack.Navigator
initialRouteName="IndivdualSetup"
mode="card"
headerMode="screen"
>
<Stack.Screen
name="IndivdualSetup"
component={IndivualScreen}
//component={<IndividualScreen individual={props.route.params.Setup} />} thought this was it but its not
options={{
header: ({ navigation, scene }) => (
<Header
title="IndivdualSetup"
tabs
tabTitleSizeRight={10}
tabRightIcon={"shape-star"}
scene={scene}
navigation={navigation}
/>
),
}}
/>
</Stack.Navigator>
);
}
组件:
import React from "react";
import { ScrollView, StyleSheet, Dimensions } from "react-native";
import { Block, Text, theme } from "galio-framework";
const { width } = Dimensions.get("screen");
const thumbMeasure = (width - 48 - 32) / 3;
export default class IndivualScreen extends React.Component {
render() {
// const {
// navigation,
// route,
// } = this.props;
// const { product } = route.params;
console.log(this.props,"props are")
return (
<Block flex center>
<ScrollView
style={styles.components}
showsVerticalScrollIndicator={false}
>
<Block flex>
<Text bold size={30} style={styles.title}>
Text here
</Text>
</Block>
</ScrollView>
</Block>
);
}
}
此组件中的控制台日志没有路由作为参数只是导航,但这只有 setParams (看起来像 v5 和更新版本中删除的 getParams。但是如果我这样做:
component={<IndividualScreen individual={props.route.params.Setup} />}
我得到:
Blockquote 错误:屏幕“IndivdualSetup”的“组件”道具的值无效。它必须是有效的 React 组件。
我的语法在任何地方看起来都是正确的,所以不确定我的问题是什么,或者如果它不起作用,因为我应该遵循更好的做法。
提前致谢!
解决方案
代替
component={<IndividualScreen individual={props.route.params.Setup} />}
你必须这样做:
component={(props)=> <IndividualScreen individual={props.route.params.Setup} />}
推荐阅读
- css - 可以使用 ngx-bootstrap 的下拉菜单附加到 ID
- android - 刷新以 startActivityForResult 开始的活动
- reactjs - 反应 useContext 返回未定义
- android - 在 android 中使用 iText 将 Firebase RecycleView 数据转换为 PDF
- docker - 错误:[] cURL 错误 7:无法连接到 localhost 端口 80:连接被拒绝 docker
- javascript - 如何从其他 npm 依赖项导入模块依赖项?
- javascript - 角度驱动表单如何禁用在数字输入时写入十进制数字的选项
- php - PHP - 如何在邮件正文中插入换行符?
- .net - 与 WCF 服务通信时值不能为空错误
- python - 如何在 Windows 10 最佳实践中不断运行 python 脚本?