Anyone using Remix? Having trouble trying to set an as prop on a Link

I tried doing the as on the root prop, the repo/stackblitz is updated. I did this on the services link, however, it doesn’t seem to apply the as there.

I removed the link prop when passing to usebehavior, to demonstrate that it stays a button. When I keep the link prop, it makes it an a like it should, but the Link behavior is still missing - no client side rendering or prefetching.

function ButtonLink_(props: ButtonProps, ref: ButtonRef) {
  const { link, ...rest } = props;
  const { plasmicProps } = PlasmicButton.useBehavior<ButtonProps>(rest, ref);
  return (
    <PlasmicButton {...plasmicProps} root={{ as: Link, props: { to: link } }} />
  );
}
const HomepageOne = (
  { toggle, ...rest }: HomepageProps,
  ref: HTMLElementRefOf<"div">
) => {
  return (
    <PlasmicHomepage
      root={{ ref }}
      {...rest}
      logo={{
        onClick: toggle,
      }}
      services={{
        as: ButtonLink,
        props: { link: "/services" },
        // as: Link,
        // props: {
        //   to: "/services",
        //   prefetch: "intent",
        // },
      }}

It looks correct now - but the as doesn’t seem to be applying

Try this (I cannot seem to fork that stackblitz for some reason):

In Homepage.tsx, we only ever render HomepageTwo.

In Button.tsx, put in this - I had to use render rather than as to remove the href prop for the remix Link to fully route client-side (if you leave an href in there it will still go back to the server, and the Button component’s default behavior is to add the href if present):

function Button_(props: ButtonProps, ref: ButtonRef) {
  const { plasmicProps } = PlasmicButton.useBehavior<ButtonProps>(props, ref);
  return (
    <PlasmicButton
      {...plasmicProps}
      root={{
        render: (rootProps, Comp) => {
          return <Link {...rootProps} to={props.link} />;
        },
      }}
    />
  );
}

One more thing - you can just set the link prop from within the studio as well, in that case you don’t need to specify any of the link props in code, you can just render a <PlasmicHomepage root={ref} {...rest}/>

Oh, on homepage.tsx I passed a toggle to the logo, if you click on the logo it will change.

The render works!!!

Thanks @yang

2022-05-17 11.35.14.gif

In the gif - I’m showing the network tab and hovering the buttons to see the prefetch in action, then later I click on them (notes for others that end up in this thread some day).

Here is what my button looks like in the above
`

``
function ButtonLink_(props: ButtonProps, ref: ButtonRef) {
  const { plasmicProps } = PlasmicButton.useBehavior<ButtonProps>(props, ref);
  return (
    <PlasmicButton
      {...plasmicProps}
      root={{
        render: ({ children, ...rest }, Comp) => (
          <Link {...(rest as any)} to={props.link || ""} prefetch="intent">
            {children}
          </Link>
        ),
      }}
    />
  );
}
<PlasmicHomepage
      root={{ ref }}
      {...rest}
      logo={{
        onClick: toggle,
      }}
      services={{
        as: ButtonLink,
        props: { link: "/services" },
        // as: Link,
        // props: {
        //   to: "/services",
        //   prefetch: "intent",
        // },
      }}