Skip to content

[Proposal] Add Navigate feature #41

@bstivenprz

Description

@bstivenprz

Hey there!

I propose to add Navigate component from react-router-dom to makeRoute, something like this:

export default function AuthenticationLayout() {
  if (!authenticated)
    return <LoginRoute.Navigate />;

  return <HomeRoute.Navigate />;
}

I made this changes for my own:

export type RouteBuilder<
  Params extends z.ZodSchema,
  Search extends z.ZodSchema
> = {
  Navigate: React.FC<
    Omit<NavigateProps, "to"> & z.input<Params> & { search?: z.input<Search> }
  >;
};

routeBuilder.Navigate = function RouteLink({
  search: linkSearch,
  ...props
}: Omit<NavigateProps, "to"> & z.input<Params> & { search?: z.input<Search> }) {
  const params = info.params.parse(props);
  const extraProps = { ...props };
  for (const key of Object.keys(params)) {
    delete extraProps[key];
  }
  return (
    <Navigate
      {...props}
      to={routeBuilder(info.params.parse(props), linkSearch)}
    />
  );
};

This would work great for this cases. Works for me.

What do you think?

hgd

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions