Code Monkey home page Code Monkey logo

Comments (11)

AleksandrHovhannisyan avatar AleksandrHovhannisyan commented on September 24, 2024 1

@equinusocio Yeah, unfortunately, that won't work as far as I can tell. You'll need to use a slot or render prop if the tag is being used for more than just decorative/wrapping purposes and needs additional props/attributes. That way, the consumer of your component can customize the rendered element as needed.

from aleksandrhovhannisyan.com.

AleksandrHovhannisyan avatar AleksandrHovhannisyan commented on September 24, 2024 1

@surjithctly I've had to do that before too. You have two options:

  1. The component accepts a level prop of 1 | 2 | 3 | ... | 6. (Preferred.)
  2. The component accepts a string prop of 'h1' | 'h2' | 'h3' | ... | 'h6'.

If you go with option 1, you can interpolate the number in a string and assert the type as an element. Same for option 2.

from aleksandrhovhannisyan.com.

equinusocio avatar equinusocio commented on September 24, 2024

Thank you for this tut! Unfortunately this solution doesn't cover the type validation of attributes.
If i set the tagName as "button" and i add the type="button" attribute it doesn't recognise it and throw a type validation error

from aleksandrhovhannisyan.com.

lianulloa avatar lianulloa commented on September 24, 2024

Also, it doesn't cover the case when you want to assign an onClick handler

from aleksandrhovhannisyan.com.

AleksandrHovhannisyan avatar AleksandrHovhannisyan commented on September 24, 2024

@lianulloa Yup! Like I mentioned above, if the rendered element needs to have any props passed to it, then a tag name won't work (because your click event's typing depends on the underlying element type, and that's dynamic—unless you do a type assertion for every possibility, which you should never try to do). What you're looking for is a ReactNode slot in this case. Then, you can easily pass along an onClick handler and just render the slot in your component.

from aleksandrhovhannisyan.com.

surjithctly avatar surjithctly commented on September 24, 2024

If I'm doing a <Heading> component, how do I restrict to use h1, h2 ... h6 only? instead of every html tag?

from aleksandrhovhannisyan.com.

tienlx97 avatar tienlx97 commented on September 24, 2024

@AleksandrHovhannisyan

  • Microsoft support dynamic tag in FluentUI v9. You can check it here
  • Drawback: it hard to get props and it remove ref, very tricky

from aleksandrhovhannisyan.com.

tienlx97 avatar tienlx97 commented on September 24, 2024

@AleksandrHovhannisyan
Another dynamic tag come from react.js beta page. It used to style Heading in MDX page. Because heading can be h1 h2 h3 h4 tag. The drawback is it doesn't runtime error check like FluentUi, but it easy to understand

from aleksandrhovhannisyan.com.

duongductrong avatar duongductrong commented on September 24, 2024

In this case, I want to pass a Component to "as" props and by some way it can detect and suggest (autocomplete) props of this component. What do I have to do to be like that?

Example:

import Button from "...."

function Parent() {
     return <AnotherComponentNotHaveButtonProps as={Button} propsColorOfButton="white" />
}

from aleksandrhovhannisyan.com.

AleksandrHovhannisyan avatar AleksandrHovhannisyan commented on September 24, 2024

@duongductrong Why not just pass the component with its props directly as a slot?

import Button from "...."

function Parent() {
     return <Component slotName={<Button propsColorOfButton="white" />} />
}

Then type slotName as ReactNode.

from aleksandrhovhannisyan.com.

duongductrong avatar duongductrong commented on September 24, 2024

@AleksandrHovhannisyan I want Component extends props of another ReactNode. Because I use nextjs and default we have component for navigation between the pages. So, if the Component can be extends any component props, then i can use their props in legacy form.

import Link from 'next/link'

export interface ButtonProps {

}

const Button : FC<ButtonProps> = ({ as: Tag = "button", children, ...props }) => {
    return <Tag {...props}>{children}</Tag> 
}

const App = ({ ...props }) => {
    return <Button as={Link} href="your-page">To another page</Button>
}

from aleksandrhovhannisyan.com.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.