页面刷新时数据丢失

时间:2021-02-15 15:07:49

标签: reactjs redux react-redux mern redux-toolkit

我正在使用 MERN 堆栈开发 Web 应用程序。我正在使用 Redux Toolkit 进行状态管理。

我在主页上显示了博客文章列表。 enter image description here 当我点击查看按钮时,我被带到该特定帖子的页面。 enter image description here 但是,当我刷新页面时,我不再看到该帖子。 enter image description here 为什么会这样?

以下是我的代码片段: src/components/ViewPost.js

import React from "react";
import { useSelector } from "react-redux";

const ViewPost = ({ match }) => {


  const post = useSelector((state) =>
    state.posts.posts.find((post) => post._id === match.params.id)
  );

  
  return (
    <div>
      {post ? (
        <div className="row mt-5">
          <div className="col-md-8 mx-auto">
            <article>
              <h3 className="">{post.title}</h3>
              <h6 className="text-muted mb-3">
                by Hemanta Sundaray | {new Date().toISOString().slice(0, 10)}
              </h6>
              <div className="col mb-3 post-border"></div>
              <p>{post.body}</p>
            </article>
          </div>
        </div>
      ) : (
        <h3 className="text-center mt-5">POST NOT FOUND</h3>
      )}
    </div>
  );
};

export default ViewPost;

0 个答案:

没有答案