The "as" Prop


Chakra Docs


⦾ The as prop is a special property in Chakra UI, which allows you to pass an element name or component to another component to determine what kind of element it should be rendered as. It provides a simple way to adjust the semantic element used for the component without having to create a new styled component or manually adjusting styles.

In Chakra UI, nearly all components accept the as prop. This allows you to create new component variants quickly and efficiently without having to create additional styled components.

Do note that while as prop provides a flexible way to adjust the component's rendered element, it may not be suitable in all scenarios. If a component has complex logic or state associated with it, it may be more suitable to build a dedicated component instead. Also, remember to ensure the semantiHTMLHTML are maintained when using the as prop for accessibility reasons.

To import the Box and Text components used below:


Rendering a Text Component as a h1: This example demonstrates rendering a Text component as a h1. Here, the component will retain all the functionalities of the Text component, but be semantically rendered as a h1.

This is a Heading using an "as" prop.

Using a Custom Component with as prop: You can also pass a custom component to the as prop. This is particularly useful if you have a component that should sometimes render as one type of element and sometimes as another. In this example, a Box is rendered as a CustomComponent. This means the Box will now have the appearance defined in the CustomComponent.

This is a custom component using an "as" prop.

Here's an example using React's built-in useState hook to manage whether a message box is displayed or not. In this example, we create a Box component and render it as a button HTML element using Chakra's as prop. We also add a hover effect using the _hover prop. The onClick prop is set to a function that toggles the isOpen state value between true and false when the button is clicked, thus controlling the visibility of the message box.

Did you know?

Creative Idea No. 1

Box as a Semantic HTML Element: Chakra's Box component is a versatile component that you can transform into any HTML element. For instance, if you need a Box component with a semantic section HTML element.

This is a section

This is the section content


Creative Idea No. 2

Transforming a Link into a Button: You can also easily transform a link to look and behave like a Chakra UI Button using the as prop. This renders a link that looks like a Chakra UI Button and directs the user.


Creative Idea No. 3

Transforming a List into a Navigation Menu: Suppose you want to create a navigation menu, but you have a list of items. Instead of manually creating a set of Link or Button components, you can use a List and transform its items into clickable links. In the following example, we are transforming each ListItem into a RouterLink (from react-router-dom). This allows each list item to behave as a link, directing the user to a different route when clicked. The to prop is dynamically set based on the item value, creating different routes for each item. This approach can also be used to quickly create dynamic breadcrumb navigations, or any similar component.


copyright © 2023 IHeartComponents |


Special thanks to Stefan Bohacek / Generative Placeholders.