inertia icon indicating copy to clipboard operation
inertia copied to clipboard

Add ability to use a custom component with <Link>

Open glafarge opened this issue 2 years ago • 2 comments

Adding the option to use either a string or a React component as a rendering option for <Link> component using as attribute. This allows for greater customization, as seen in the examples below:

Use HTML tag as a wrapper (current implementation)

import { Link } from '@inertiajs/react'

<Link href="/logout" method="post" as="button" type="button">Logout</Link>

// Renders as... 
<button type="button">Logout</button>

Use a custom component

import { Link } from '@inertiajs/react'
import { MyCustomButton } from "./buttons/my-custom-button";

<Link href="/logout" method="post" as={MyCustomButton} foo="bar">Logout</Link>

// Renders as... 
<MyCustomButton foo="bar">Logout</MyCustomButton>

This addresses issue #1082 and improves overall functionality. Any feedback or suggestions for improvement are welcomed!

glafarge avatar Jan 17 '23 18:01 glafarge

Wouldn't it be cleaner to make MyCustomButton wrap the Link component so that you don't need two imports when using it? The button component could conditionally render a Link or button depending on whether the href prop has been provided.

jessarcher avatar Feb 13 '23 23:02 jessarcher

@jessarcher Same issue here. I have a basic PrimaryButton component. I use it for different usages, and sometimes I would like to use it as a Inertia Link. It make sense to me to be able to pass Link in its as props. It's also the pretty standard behavior of such as props. (example with EmotionCSS or HeadlessUI).

mathieutu avatar Mar 19 '23 21:03 mathieutu