Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'as' props (elementType) in react-bootstrap Components?

In react-bootstrap documentation, there is 'as' props in API of every component with Type elementType. I can't figure out how to use it in component. I tried passing in React components and "<div>test</div>" as "as" props but didn't work. I tried to search for any example or implementation but could not find any. Thanks.

like image 382
mustafa1993 Avatar asked Oct 24 '25 17:10

mustafa1993


2 Answers

You are passing component type that will be instantiated with JSX. Internally it looks something alike

function Button(props) {
 const ComponentType = as;
 .......
  <ComponentType ......>

So as you know it will work if it's a string with tagname of native element("div", "table" etc) or valid React component(class inherited React.Component or function for functional component)

like image 54
skyboyer Avatar answered Oct 26 '25 06:10

skyboyer


I needed this to customize the tab of a nav component

     <Nav className='tabs-div' variant='tabs' >
        <Nav.Item  >
          <Nav.Link className='pending'  eventKey={-1}>Pending</Nav.Link>
        </Nav.Item>
        <Nav.Item>
          <Nav.Link className='positive' eventKey={1}>Positive</Nav.Link>
        </Nav.Item>
        <Nav.Item>
          <Nav.Link className='negative' eventKey={0}>Negative</Nav.Link>
        </Nav.Item>
      </Nav>

you can understand from the code that the tab will be as normal. but I needed another costumization with notification on the tab icons ....

so I created a component like this

import React from 'react'

class BtnPending extends React.Component{


    render(){
        return(
            <button className='btn btn-warning' >Pending</button> // this is just for the sake of demo
        )
    }
}


export default BtnPending;

Nav.Link will be changed to this

<Nav.Link as={()=>{return(<BtnPending/>)}} eventKey={-1}>Pending</Nav.Link>

for Tab in react-bootstrap you can also use the argument title

like image 23
Sadak Avatar answered Oct 26 '25 08:10

Sadak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!