Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
cómo hacer que la propiedad "href" de "Link" de Next.js sea opcional

Hice Next.js Link como una función React personalizada con mecanografiado, ya que se requiere la propiedad "href" para que "Link" pueda llamarlo donde quiera. así que no pude usarlo como un botón que funciona como un botón de envío en formularios

 import NextLink, { LinkProps } from "next/link"; import { HTMLProps, FC } from "react"; export const LinkButton: FC<LinkProps & HTMLProps<HTMLAnchorElement>> = ({ as, children, href, replace, scroll, shallow, passHref, className, title, onClick, ...rest }) => ( <NextLink as={as} href={href} passHref={passHref} replace={replace} scroll={scroll} shallow={shallow} > <a className={className} {...rest}> {children || title} </a> </NextLink> );

y cuando lo usé en algún lugar me dio este error

 <LinkButton onClick={() => alert("hello")} title="Find Events" /> Property 'href' is missing in type '{ onClick: () => void; title: string; }' but required in type 'LinkProps'.

¿Cómo podría hacer que href sea opcional? Entonces, si no se llama, puedo agregar condicionalmente una etiqueta "" excepto la etiqueta ""

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede omitir la propiedad href y definirla como opcional:

 Omit<LinkProps & HTMLProps<HTMLAnchorElement>, "href"> & { href?: string }

y luego dentro de la desestructuración de los accesorios del componente, debe especificar el valor predeterminado

 ... href = "", ...
about 4 years ago · Juan Pablo Isaza Report

0

Para que href nunca esté vacío, puede obtener su ruta y devolverla cuando no esté definida

 const router = useRouter()
 <NextLink as={as} href={href ? href : router.asPath} // <--- passHref={passHref} replace={replace} scroll={scroll} shallow={shallow} >
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!