reactjs - 组件中的 this.props 不包含导航属性
问题描述
我想从一个屏幕导航到另一个屏幕,this.props.navigation.navigate('Second Screen')
但属性“导航”不包含在道具中。获取错误:类型 'Readonly<{}> 和 Readonly<{ children?: ReactNode; 上不存在属性'navigation' }>'
import React, { Component } from "react";
import styles from "./style";
import { Keyboard, Text, View, TextInput, TouchableWithoutFeedback, KeyboardAvoidingView } from 'react-native';
import { Button } from 'react-native-elements';
export default class SignInScreen extends Component {
render() {
return (
<KeyboardAvoidingView style={styles.containerView} behavior="padding">
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={styles.loginScreenContainer}>
<View style={styles.loginFormView}>
<Text style={styles.logoText}>Book Market</Text>
<TextInput placeholder="Email" placeholderTextColor="#c4c3cb" style={styles.loginFormTextInput} />
<TextInput placeholder="Password" placeholderTextColor="#c4c3cb" style={styles.loginFormTextInput} secureTextEntry={true} />
<Button
buttonStyle={styles.loginButton}
onPress={this.props.navigation.navigate('Home')}
title="Login"
/>
</View>
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
);
}
}
解决方案
您只能通过父元素访问它。将导航作为道具传递给其子级以使用它
例如:
<Child navigation={this.props.navigation} />
推荐阅读
- javascript - 如何避免 D3 强制布局中的链接(Egdes)重叠
- spring-boot - Spring data mongodb reactive:使用 spring.data.mongodb.uri 或实现 AbstractReactiveMongoConfiguration.getDatabaseName
- parse-platform - 在 Parse-ACL 或 CLP 上与许多用户共享数据库访问权限?
- docker - NestJS .env 未连接到 Docker 容器数据库
- r - 有没有办法从 R 访问谷歌驱动器中的文件?
- java - 制作孤岛大leetcode - 发现错误Java
- php - 在 laravel 中配置路由 apiResource
- reactjs - 团队应用程序是否支持流畅的 UI 而不是北极星?
- node.js - 如何在 Sequelize 中为嵌套模型应用不同的排序
- sqlite - 使用通配符对值进行分组