Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hasPathChanged on the Guard component configurable #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions package/src/Guard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ interface GuardsResolve {
redirect: RouteRedirect;
}

const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta, render }) => {
const Guard: React.FunctionComponent<GuardProps> = ({
children,
component,
meta,
render,
pathChanged,
name,
}) => {
const routeProps = useContext(RouterContext);
const routePrevProps = usePrevious(routeProps);
const hasPathChanged = useMemo(
() => routeProps.location.pathname !== routePrevProps.location.pathname,
[routePrevProps, routeProps],
);
const hasPathChanged = useMemo(() => {
if (pathChanged && typeof pathChanged === 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be more clear if we named this variable checkPathChanged so you know from a glance it should be a function

return pathChanged(routePrevProps, routeProps, name);
}
return routeProps.location.pathname !== routePrevProps.location.pathname;
}, [routePrevProps, routeProps]);
const fromRouteProps = useContext(FromRouteContext);

const guards = useContext(GuardContext);
Expand Down
8 changes: 7 additions & 1 deletion package/src/GuardedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const GuardedRoute: React.FunctionComponent<GuardedRouteProps> = ({
ignoreGlobal,
loading,
meta,
pathChanged,
render,
path,
...routeProps
Expand All @@ -32,7 +33,12 @@ const GuardedRoute: React.FunctionComponent<GuardedRouteProps> = ({
<GuardContext.Provider value={routeGuards}>
<ContextWrapper<PageComponent> context={LoadingPageContext} value={loading}>
<ContextWrapper<PageComponent> context={ErrorPageContext} value={error}>
<Guard name={path} component={component} meta={meta} render={render}>
<Guard
name={path}
pathChanged={pathChanged}
component={component}
meta={meta}
render={render}>
{children}
</Guard>
</ContextWrapper>
Expand Down
5 changes: 5 additions & 0 deletions package/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export interface BaseGuardProps {

export type PropsWithMeta<T> = T & {
meta?: Meta;
pathChanged?: (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding the type here, we'll want to add it to the GuarderRoute and GuardProps below

routePrevProps: RouteComponentProps,
routeProps: RouteComponentProps,
path?: string | string[],
) => boolean;
};

export type GuardProviderProps = BaseGuardProps;
Expand Down