简体   繁体   English

更改页面时 redux 重置状态

[英]redux reset state when changing pages

So i've implemented infinite scroll with redux.所以我用 redux 实现了无限滚动。 Everythings works its just that when I change pages then go back to the home page, it shows the posts i scrolled down to times two.一切都有效,只是当我更改页面然后返回主页时,它会显示我向下滚动到两次的帖子。 I'm assumming the issue here is with the way i setup my redux.我假设这里的问题与我设置 redux 的方式有关。 I've been scouring here and the only thing that sounded logical to me was to reset the state on component unmount.我一直在这里搜索,对我来说唯一听起来合乎逻辑的事情是在组件卸载时重置状态。 I've created it in my actions and added it to my useEffect and it still dont work.我已经在我的操作中创建了它并将它添加到我的 useEffect 中,但它仍然不起作用。 Is there a way when I leave my home page, it will reset so that when i go back to my home page it will fetch all posts again?有没有办法当我离开我的主页时,它会重置,以便当我回到我的主页时,它会再次获取所有帖子? Im stuck.我卡住了。 Help please请帮忙

HomePage主页

const [page, setPage] = useState(1);
const dispatch = useDispatch();
const { loading, posts, total } = useSelector((state) => state.posts);

useEffect(() => {
    dispatch(getPosts(page));

    return () => {
      dispatch(resetPosts());
    };
  }, [dispatch, page]);

Actions行动

export const getPosts = (page) => async (dispatch) => {
  try {
    const {
      data: { posts, totalPosts },
    } = await api.fetchPosts(page);

    dispatch({
      type: PC.FETCH_ALL_POSTS_SUCCESS,
      payload: { posts, totalPosts },
    });
  } catch (error) {
    dispatch({ type: PC.FETCH_ALL_POSTS_FAIL, payload: error.response });
  }
};
export const resetPosts = () => {
  return { type: PC.RESET_POSTS };
};

Reducers减速机

export const postsReducer = (state = { posts: [] }, action) => {
  switch (action.type) {
    case PC.FETCH_ALL_POSTS_REQUEST:
      return { loading: true };
    case PC.FETCH_ALL_POSTS_SUCCESS:
      return {
        loading: false,
        posts: [...state.posts, ...action.payload.posts],
        total: action.payload.totalPosts,
      };
    case PC.FETCH_ALL_POSTS_FAIL:
      return { loading: false, error: action.payload };
    case PC.RESET_POSTS:
      return state;

It looks like you are storing your redux state incorrectly.看起来您存储的 redux 状态不正确。 Try edited your logic where you are using the spread operator.尝试在您使用扩展运算符的地方编辑您的逻辑。 It should probably be应该是

return {
        loading: false,
        posts: action.payload.posts,
        total: action.payload.totalPosts,
      }

This way you are actually updating your state and not making all of your state just one big posts array.这样你实际上是在更新你的状态,而不是让你的所有状态只是一个大的帖子数组。 I can see what you were trying to do, but that is only really needed if you are doing your return object like我可以看到你想做什么,但只有当你在做你的返回对象时才真正需要这样做

return { ...state, posts: action.payload.data } 

and need to maintain other state variables alongside the posts.并且需要在帖子旁边维护其他状态变量。

A tool that could help you debug this better in the future is the redux chrome dev tools. redux chrome dev 工具可以帮助您在未来更好地调试它。 https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM