如何解决如何使用React Redux和Firebase Connect在帖子中更改评论
我正在尝试克隆instagram,目前我停留在评论部分,当我尝试添加评论时,会将相同评论添加到所有帖子中。我将我的评论存储在firestore中,并且正确保存了该评论,但我无法弄清楚如何管理状态,因此只有那些帖子才能反映添加评论的更改。
这是showcomments组件的以下代码,它将在另一个组件中使用。 谁能帮助我解决这个问题。
import React from "react";
import { connect } from "react-redux";
import { firestoreConnect } from "react-redux-firebase";
import { compose } from "redux";
import { makeStyles } from "@material-ui/styles";
const useStyles = makeStyles((theme) => ({
comment: {
marginTop: "1rem",},postCaption: {
...theme.typography.postCaption,username: {
...theme.typography.username,}));
function ShowComments(props) {
const classes = useStyles();
const { comments } = props;
const renderComments = () => {
if (comments) {
return comments.map((comment) => {
return (
<h3 key={comment.id}>
{comment.username}{" "}
<span className={classes.postCaption}>{comment.comment}</span>
</h3>
);
});
}
};
return (
<div>
{console.log(props.postId)}
{renderComments()}
</div>
);
}
const mapStateToProps = (state) => {
console.log(state);
return {
comments: state.firestore.ordered.comments,};
};
export default compose(
connect(mapStateToProps),firestoreConnect((props) => {
return [
{
collection: "instagramPosts",doc: props.postId,subcollections: [{ collection: "comments" }],storeAs: "comments",];
})
)(ShowComments);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。