In my Home component(I call it Home Page!) I am using Cards.JS component which has posts attribute as shown in following code.
const Home = () => { const dispatch = useDispatch() const isLoading = useSelector(state => state.isLoading) const currentPage = useSelector((state) => state.idFor.currentPageHome) const homePosts = useSelector((state) => state.posts) useEffect(() => { dispatch(setIsLoading(true)) dispatch(getAllPosts(currentPage)) }, [dispatch, currentPage]) return ( isLoading ? ( <Loader type="ThreeDots" color="#000000" height={500} width={80} /> ) : ( <Cards posts={homePosts} setCurrentPage={setCurrentPageHome} currentPage={currentPage} pageName={"LATEST"} /> ) ) } And Cards.Js is as following
const Cards = ({ posts, setCurrentPage, currentPage, pageName }) => { console.log('Cards.JS called', posts); const dispatch = useDispatch() useEffect(() => { dispatch(setIsLoading(false)) }) const handleNextPage = () => { dispatch(setIsLoading(true)) dispatch(setCurrentPage(currentPage + 1)) } const handlePreviousPage = () => { dispatch(setIsLoading(true)) dispatch(setCurrentPage(currentPage - 1)) } return ( <div className="container"> <h4 className="page-heading">{pageName}</h4> <div className="card-container"> { posts.map(post => <Card key={post._id} post={post} />) } </div> <div className="page-div"> {currentPage !== 1 ? <span className="previous-page" onClick={handlePreviousPage}><</span> : null} <span className="next-page" onClick={handleNextPage}>></span> </div> </div> ) } My Problem: When i come back to home page useEffect is called everytime and request same data to back-end which are already avaliable in Redux store.
Thanks in Advance :)