Skip to content

Commit

Permalink
feat: header state management using HoC
Browse files Browse the repository at this point in the history
  • Loading branch information
DPS0340 committed Nov 16, 2022
1 parent 3af6b43 commit 2707225
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/hocs/with-header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useAtom } from 'jotai';
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import currentPage from '../atoms/current-page';
import { WithHeaderProps } from '../types/hocs';

export const WithHeader: React.FC<WithHeaderProps> = ({ children, datum }) => {
const location = useLocation();
const [, setPage] = useAtom(currentPage);

useEffect(() => {
if (location.pathname === datum.uri) {
setPage(datum);
}
}, [datum, location.pathname]);

return <>{children}</>;
};
6 changes: 6 additions & 0 deletions src/types/hocs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import IRouterDatum from '../../interfaces/router';

export type HoCProps = {
children?: React.ReactNode;
};

export type WithHeaderProps = HoCProps & {
datum: IRouterDatum;
};

0 comments on commit 2707225

Please sign in to comment.