From 6cdc14d6d56080b0f44a09911b9258b6bdf48911 Mon Sep 17 00:00:00 2001 From: Ken <gialap2002@gmail.com> Date: Fri, 16 Dec 2022 10:38:18 +0700 Subject: [PATCH] feat: add reset page for home --- src/pages/home/homePageSlice.ts | 9 +++++++-- src/pages/home/index.tsx | 7 ++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/pages/home/homePageSlice.ts b/src/pages/home/homePageSlice.ts index 6c9b22d..ebdb7d4 100644 --- a/src/pages/home/homePageSlice.ts +++ b/src/pages/home/homePageSlice.ts @@ -17,7 +17,11 @@ export const getNews = createAsyncThunk( const home = createSlice({ name: "home", initialState, - reducers: {}, + reducers: { + handleResetNews: (state) => { + state.newsData = []; + }, + }, extraReducers: (builder) => { builder.addCase(getNews.fulfilled, (state, action: PayloadAction<any>) => { const { collection } = action.payload.data; @@ -31,5 +35,6 @@ const home = createSlice({ }, }); -const { reducer } = home; +const { reducer, actions } = home; +export const { handleResetNews } = actions; export default reducer; diff --git a/src/pages/home/index.tsx b/src/pages/home/index.tsx index 7dc4d21..53a91b6 100644 --- a/src/pages/home/index.tsx +++ b/src/pages/home/index.tsx @@ -3,7 +3,7 @@ import { homeSelector } from "app/selectors"; import WrapperContainer from "components/WrapperContainer"; import React, { useEffect, useState } from "react"; import Newspaper from "../../components/Newspaper"; -import { getNews } from "./homePageSlice"; +import { getNews, handleResetNews } from "./homePageSlice"; import { IPagination } from "pages/interface"; import { useInView } from "react-intersection-observer"; import { CircularProgress } from "@mui/material"; @@ -50,6 +50,11 @@ const HomePage = () => { } }, [inView]); + useEffect(() => { + dispatch(handleResetNews()); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + return ( <main className="homePage"> <WrapperContainer> -- 2.18.1