javascript - 类型错误:_This.props 未定义
问题描述
我是 React Native 的新手,请帮我解决这个问题:TypeError: _props.goToScreen is not a function,请帮助我提前谢谢
Profile.js 我想点击 GotoHome 按钮并转到另一个页面
export default class Profile extends Component {
_GotoHome = () => {
this.props.goToScreen('UploadProduct');
};
render() {
return (
<ListItem icon>
<Left>
<Text>List All Uploaded Product</Text>
</Left>
<Body />
<Right>
<Icon
active
name="angle-right"
type="FontAwesome"
onPress={() => this._GotoHome()}
/>
</Right>
</ListItem>
);
}
}
这是我在个人资料页面 Navigation\index.js 中的导航
import {createDrawerNavigator, createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import {createBottomTabNavigator} from 'react-navigation-tabs';
import UploadProductScreen from './UploadProductScreen';
const ProfileStackNavigator = createStackNavigator(
{
Profile: {
screen: ProfileScreen,
},
UploadProduct: {
screen: UploadProductScreen,
},
},
{headerMode: 'none'},
);
export default createAppContainer(ProfileStackNavigator );
这里是我的 uploadProductScreen 页面
import {UploadProduct} from '@container';
class UploadProductScreen extends Component {
render() {
const {navigate} = this.props.navigation;
return (
<UploadProduct
navigation={this.props.navigation}
goToScreen={(screen, EventId) => navigate(screen, {ItemId: EventId})}
/>
);
}
}
export default UploadProductScreen;
解决方案
在您的 Profie 组件中定义构造函数以使用道具。
constructor(props) {
super(props);
}
我也使用这样的东西导航到另一个屏幕。
this.props.navigation.navigate("UploadProduct"); // here UploadProduct is the key yo have defined in stack navigator
推荐阅读
- pandas - 在 Pandas 中绘制日期,并用线连接它们的持续时间
- video - 将 MPEG-1 转码为 MPEG-H (H.265/HEVC)
- c# - 使用实体框架核心从自相关表中排除重复结果
- android - 构建基于 kotlin 的 Android 应用程序时的 gradle 问题!未能通知项目评估监听器
- jenkins - 运行单元测试并在 jenkins 中提供单元测试报告
- linux - 从 .csv 文件中读取值并使用 Shell 脚本执行计算
- python - 如何在 node.js 应用程序中上传图像,用 python 处理它,然后将结果发回
- video-streaming - 各种应用程序和网络浏览器中视频会议的 CPU 消耗高
- google-translate - 有没有办法在谷歌翻译中使用自定义词
- java - Java List 无需任何操作即可删除项目