Comments (11)
@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.
@surjithctly I've had to do that before too. You have two options:
- The component accepts a
level
prop of1 | 2 | 3 | ... | 6
. (Preferred.) - 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.
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.
Also, it doesn't cover the case when you want to assign an onClick
handler
from aleksandrhovhannisyan.com.
@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.
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.
- 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.
@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.
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.
@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.
@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)
- Add Comments to a Static Site with Netlify Functions and the GitHub API HOT 7
- Optimizing Images with the 11ty Image Plugin HOT 39
- Configuring Web Fonts in 11ty with Global Data
- Keeping Focus in Place with Load-More Buttons HOT 3
- Writing Better CSS HOT 7
- Creating a Fluid Type Scale with CSS Clamp HOT 17
- HTML Input Validation Without a Form HOT 1
- copyCode.js error in line: const code = codeBlocks[index].innerText; HOT 1
- Comments are getting truncated after 30 HOT 2
- Design Mode in 11ty HOT 2
- Image attribution not displaying HOT 1
- Feature Request: Front Matter Format For Local Image HOT 1
- Creating an Accessible Image Carousel HOT 10
- Feature Request: Order comments from older to newer HOT 1
- Lamp icon use? HOT 2
- figure.html params
- postImage.html params
- postImage.html params HOT 2
- RSS feed issue HOT 6
- After endless search I found this article. A lifesaver! Thanks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aleksandrhovhannisyan.com.