I've been attempting to render a react-bootstrap component within a custom navBar component in react. I have a recursive function set up which is supposed to run in react render and drill down until there are no nav Item components under the NavDropDown Components. Currently when any attempts at drilling down return as undefined and don't display in the nav bar.
I've tried reformatting my react code in numerous ways, including/removing brackets, switching to plaintext, etc.
code below:
navContent = {[
{
type : "link",
target: "/",
content: "Home"
},
{
type: "dropdown",
title: "CLICK ME",
content: [
{type : "item",
target: "/",
content: "home"
},
{type : "item",
target: "/",
content: "home"
}
]
},
{
type: "form",
formType: "text",
placeholder: "search",
className: "",
buttonType: "submit",
buttonContent: "Submit"
},
]}
export default class Navigation extends React.Component {
constructor(props){
super(props);
this.state = {
}
}
getNavItem(navItem){
switch(navItem.type){
case 'link':
return <Nav.Link><Link to={navItem.target}>{navItem.content}</Link></Nav.Link>
case 'dropdown':
return <NavDropdown id="basic-nav-dropdown" title={navItem.title}>
{navItem.content.map(item => {this.getNavItem(item)})}
</NavDropdown>
case 'form':
return <Form inline> <FormControl type={navItem.formType} placeholder={navItem.placeholder} className={navItem.className}/><Button type={navItem.buttonType}>{navItem.buttonContent}</Button></Form>
case 'item':
return <NavDropdown.Item><Link to={navItem.target}>{navItem.content}</Link></NavDropdown.Item>
}
}
render(
){
return(
<Navbar bg="light" expand="lg">
<Link to="/"><Navbar.Brand>Home Placeholder</Navbar.Brand></Link>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
{this.props.navContent.map(navItem => this.getNavItem(navItem))}
</Nav>
</Navbar.Collapse>
</Navbar>
)
}
}
Ideally when the case switch hits dropdown in the getNavItem function it should run the function again iterating down into the content key of the dropdown object and render both objects inside of it under the dropdown. Currently the dropdown content doesn't render.
<Nav>render correctly now? If not, it's not infinitely easier to debug your code in terms of where things might have gone wrong. Does it work? Cool: add a single child, and see what happens