首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能用按钮从我的数据库中删除一个帖子。反应图

我不能用按钮从我的数据库中删除一个帖子。反应图
EN

Stack Overflow用户
提问于 2022-11-15 02:58:39
回答 1查看 24关注 0票数 0

嘿,伙计们,我正试图从我的数据库中删除一篇文章,但我有问题,让这个按钮工作。我将整个函数移到我的feed文件中,创建了一个不同的函数,但仍然无法工作。我真的很感谢你的帮助。

代码语言:javascript
复制
…All the imports

function DeletePostBtn ({ postId }){
  const navigate = useNavigate;`
  const { _id } = useParams();`

  const [deletePost] = useMutation(REMOVE_POST, {
    variables: {id: _id },
    onCompleted: () => navigate('/'),
    refetchQueries: [{ query: QUERY_POSTS }],
  });
    
  // remove handler
  const removeHandler = async (event) => {
    event.preventDefault();
    try {
      await deletePost({ variables: {_id} });
    } catch (e) {
      console.error(e);
    }
  };

  return (
    <Button onClick={() => {
      removeHandler();
      alert('clicked');
    }}>
      <Trash/>
   </Button>  
  );
}

export default DeletePostBtn;

我在我的饲料页上叫它

代码语言:javascript
复制
const Feed = ({ posts }) => {
  return (
    <div className="feed container gx-12">
      <div className="feedWrapper row">
        <AddPost />
        {posts &&
          posts.map((post) => (
            <Card key={post._id}>
              <Card.Header>
                <Row>
                  <Col xs={1}>
                    <div className="">
                      <Card.Img
                        src={Bot}
                        className="rounded-circle"
                        style={{ width: '50px' }}
                      />
                    </div>
                  </Col>
                  <Col>
                    <Link
                      to={`/profile/${post.username}`}
                      style={{ fontWeight: 750 }}
                      className="text-start"
                    >
                      u/{post.username}
                    </Link>
                  </Col>
                  <Col className="text-end">
                    <Dropdown>
                      <Dropdown.Toggle
                        variant="text-dark"
                        id="dropdown-basic"
                        size="lg"
                        bsPrefix
                        className="dropdown"
                      >
                        <ThreeDots />
                      </Dropdown.Toggle>
                      <Dropdown.Menu>
                        <Dropdown.Item href="#/action-1">
                          <Stack direction="horizontal">
                            <DeletePost />
                            DELETE
                          </Stack>
                        </Dropdown.Item>
                        <Dropdown.Item href="#/action-2">EDIT</Dropdown.Item>
                        <Dropdown.Item href="#/action-3">PROFILE</Dropdown.Item>
                        <Dropdown.Item href="#/action-3">
                          SUBCODEIT
                        </Dropdown.Item>
                      </Dropdown.Menu>
                    </Dropdown>
                  </Col>
                </Row>
              </Card.Header>
              <Card.Title>{post.enteredTitle}</Card.Title>
              <Card.Body>
                <Card.Text>{post.enteredText}</Card.Text>
              </Card.Body>
              <Card.Footer className="cardFooter">
                <div>
                  Comments: {post.commentCount} || Click to{' '}
                  {post.commentCount ? 'Add' : 'Start'} comments.
                </div>
                <div>{post.createdAt}</div>
              </Card.Footer>
            </Card>
          ))}
        ;
      </div>
    </div>
  );
};
EN

回答 1

Stack Overflow用户

发布于 2022-11-17 00:09:11

更改组件以使用postId参数而不是useParams()

代码语言:javascript
复制
function DeletePostBtn ({ postId }){
  const navigate = useNavigate;`
  
  const [deletePost] = useMutation(REMOVE_POST, {
    variables: {id: postId },
    onCompleted: () => navigate('/'),
    refetchQueries: [{ query: QUERY_POSTS }],
  });
  …
}

然后将postId传递给.map()中的组件

代码语言:javascript
复制
<DeletePost postId={post._id}/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74440151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档