javascript - undefined 不是递归组件上的对象(评估“_this2.props.navigation.navigate”)
问题描述
设置路由器后:
PostShow > 评论 > 评论 > 回复 postShow 属于 stackNavigator!
当点击评论组件去回复时
显示此错误:
undefined 不是对象(评估 '_this2.props.navigation.navigate')
在搜索文档和 react-navigation 问题页面以及这里之后
该解决方案似乎将 navigation={this.props.navigation} 放在子组件上,因为它不在堆栈内。但经过一番尝试后仍然显示相同的错误!
然后其他解决方案是在父组件上使用 withNavigation ,仍然不重定向到回复屏幕!
那么,当组件是递归的或不在堆栈或任何导航组件下时,有人可以澄清如何正确使用导航?
<router>
export const HomeStack = createStackNavigator({
Home: {
screen: Home
},
PostShow: {
screen: PostShow,
navigationOptions: ({ navigation }) => ({
title: `${navigation.state.params.post.title}`,
headerTitleStyle,
headerStyle
})
}
});
The postSHow component with:
<postShow>
import React, { Component } from "react";
import {
View,
Text,
Image,
KeyboardAvoidingView,
TextInput,
StyleSheet,
ScrollView,
TouchableOpacity,
} from "react-native";
import Comments from "../components/Comments";
class PostShow extends Component {
constructor(props) {
super(props);
this.state = {
title: "",
body: "",
comments: [],
comment: "",
authToken: "",
position: 1
};
}
componentDidMount() {
this.getComments();
}
async getComments();
render() {
const { title, body } = this.state;
return (
<View style={{ flex: 1 }}>
<ScrollView>
<Text>{title}</Text>
<Comments
comments={this.state.comments}
//navigation={this.props.navigation}
//navigate={this.props.navigation.navigate}
/>
</ScrollView>
<View>
<View >
<KeyboardAvoidingView
keyboardVerticalOffset={80}
behavior={"padding"}
style={{
flex: 1
}}
>
<TextInput
value={this.state.comment}
underlineColorAndroid="transparent"
placeholder="Type something nice"
onChangeText={text => this.setState({ comment: text })}
/>
<TouchableOpacity
style={styles.button}
onPress={this.submitComment.bind(this)}
>
<Text>Send</Text>
</TouchableOpacity>
</KeyboardAvoidingView>
</View>
</View>
</View>
);
}
}
export default PostShow;
The comments/comment
<comments/comment>
import Comment from "./Comment";
class Comments extends Component {
render() {
var comments =
this.props.comments &&
this.props.comments.map(comment => {
return (
<View key={comment.id} style={styles.commentContainer}>
<Comment
// navigate={this.props.navigation.navigate}
navigation={this.props.navigation}
comment={comment}
/>
</View>
);
});
return <View>{comments}</View>;
}
}
export default Comments;
I'd like to on comment component be able to click on reply and redirect to the screen Reply
import React, { Component } from "react";
import { View, Text } from "react-native";
import Comments from "./Comments";
import Reply from "./Reply";
import { withNavigation } from "react-navigation";
class Comment extends Component {
render() {
const comment = this.props.comment;
const { navigation } = this.props;
return (
<View>
<Text style={styles.body}>{comment.comment} </Text>
<Text
style={{ color: "blue" }}
onPress={() => this.props.navigation.navigate("Reply")}
>
Reply
</Text>
<Comments
// navigation={this.props.navigation}
// navigate={this.props.navigation.navigate}
comments={comment.replies}
/>
</View>
);
}
}
export default Comment;
解决方案
如果你Comment
在Comments
课堂上使用,你应该转发data
.
<Comment
// navigate={this.props.navigation.navigate}
navigation={this.props.navigation.navigate("Reply")}
comment={comment}
/>
...
const { navigation } = props;
return (
<View>
<Text style={styles.body}>{comment.comment} </Text>
<Text
style={{ color: "blue" }}
onPress={() => navigation}
>
推荐阅读
- windows - 在命令行中解码 base64 文本。得到错误
- discord.py - 我正在尝试创建一个 unban 命令,但出现了这个错误。“缩进中制表符和空格的使用不一致”
- c++ - 如何将 ASCII 表格打印为表格
- excel - 冒泡排序集合并忽略大小写敏感
- c++ - 以下代码的输出是什么?(异常处理 C++)
- rest - Delphi Datasnap REST 读取请求头 TWebrequest
- java - redhat ubi-openjdk的包安装命令
- chart.js - Chart.js minBarLength 是否适用于堆叠条?
- r - 在代码繁重的统计书籍中加快 bookdown 构建的选项
- python - 在 Python Pandas 中,有没有办法在一行中多行相邻?保持秩序