Displaying React components using Bootstrap in the navigation bar

I'm feeling a little stuck here. I'm currently working with React Bootstrap and the Navigation component.

One of the routes in my application is the dashboard, which is only accessible after logging in.

When a user navigates to the dashboard, I have managed to achieve the following: https://i.sstatic.net/oH0ld.jpg

However, I would like to accomplish something more like this: https://i.sstatic.net/VnnQl.jpg

My goal is for the user to click on "BOM X-ray" and have it render small components Children 1, 2, and 3.

Here is the code that renders the first image:

import React from 'react'
import Bom from './bomxray/Indexbom';

import Nav from 'react-bootstrap/lib/Nav';
import NavItem from 'react-bootstrap/lib/NavItem';
import NavDropdown from 'react-bootstrap/lib/NavDropdown';
import MenuItem from 'react-bootstrap/lib/MenuItem';

class Dashboard extends React.Component {
   handleSelect(event, selectedKey) {
       //event.preventDefault();
       alert('selected ' + selectedKey);
   }
render() {
    return (
        <Nav bsStyle="tabs" activeKey={1} onSelect={this.handleSelect}>
            <NavItem eventKey={1} >Bom X-Ray</NavItem>
            <NavItem eventKey={2} title="Item">Calculation</NavItem>
            <NavItem eventKey={3} disabled>NavItem 3 content</NavItem>
            <NavDropdown eventKey={4} title="Dropdown" id="nav-dropdown">
                <MenuItem eventKey="4.1">Action</MenuItem>
                <MenuItem eventKey="4.2">Another action</MenuItem>
                <MenuItem eventKey="4.3">Something else here</MenuItem>
                <MenuItem divider />
                <MenuItem eventKey="4.4">Separated link</MenuItem>
            </NavDropdown>
        </Nav>
    );
 }
export default Dashboard

My plan is to create a component like this:

import Bom from './bomxray/Indexbom';

The Bom component will contain the Children inside, which will handle CRUD Ajax requests to a server.

I've tried things like this (FAIL):

 <NavItem eventKey={1} <Bom />>Bom X-Ray</NavItem>

And this (it renders, but everything is messed up):

<NavItem eventKey={1} >Bom X-Ray</NavItem> <Bom />

If I'm on the right track, any ideas on how to proceed would be appreciated. If I'm completely off track, please point me in the right direction :)

EDIT (Added possible solution): It just occurred to me, I do have:

handleSelect(event, selectedKey) {       
    alert('selected ' + selectedKey);
}

Could I dynamically render the component based on what was clicked in the handleSelect function?

Answer №1

To maintain the formatting of your Nav, my recommendation is to render the <Bom/> component outside of the <Nav> component. You can manage which component is displayed by utilizing the handleSelect function and setting an activeKey in the component's state.

 class Dashboard extends React.Component {
    getInitialState(){
        return({activeKey: 1})
    }
    handleSelect(selectedKey, event) {
        //event.preventDefault();
        alert('selected ' + selectedKey);
        this.setState({activeKey: selectedKey});
    }
    render() {
        return (
            <div>
                <Nav bsStyle="tabs" activeKey={this.state.activeKey} onSelect={this.handleSelect}>
                    <NavItem eventKey={1} >Bom X-Ray</NavItem>
                    <NavItem eventKey={2} title="Item">Calculation</NavItem>
                    <NavItem eventKey={3} disabled>NavItem 3 content</NavItem>
                    <NavDropdown eventKey={4} title="Dropdown" id="nav-dropdown">
                        <MenuItem eventKey="4.1">Action</MenuItem>
                        <MenuItem eventKey="4.2">Another action</MenuItem>
                        <MenuItem eventKey="4.3">Something else here</MenuItem>
                        <MenuItem divider />
                        <MenuItem eventKey="4.4">Separated link</MenuItem>
                    </NavDropdown>
                </Nav>

                {this.state.activeKey == 1 ? <Bom/> : null}
                {this.state.activeKey == 2 ? <CalculationComponent/> :null}
            </div>

        );
     }
 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a 404 Error on all routes except the home page when working with the Express Application Generator

While working on developing a day planner, I encountered an issue with the routes. I am consistently receiving a 404 error for any route other than the main Home page route (index or "/"). Below is the content of the app.js file: var express = require(&ap ...

Where can I find the login credentials for setting up the email form API?

I've been working on implementing a form in Nextjs using 'React Hook Form'. So far, everything seems to be in order. However, I'm unsure where to retrieve the 'user' and 'pass' values for the API in order to send the ...

I recently started learning Next.js and I'm interested in organizing my website with multiple page folders but still using a single dynamic route labeled as [id]

my-nextjs-app/ |-- .next/ |-- node_modules/ |-- public/ |-- src/ | |-- components/ | |-- men/ | | |-- [id]/ | | |-- page.tsx # Specific ID static page for Men's category | | |-- page.tsx # General stat ...

New options instead of <dl compact> that are not outdated

One feature I remember fondly from Internet Explorer is the ability to create compact definition lists using code like this: <dl compact> <dt>f</dt> <dd>Firefox</dd> <dt>i</dt> <dd>Int ...

Unable to navigate through bootstrap dropdown items using keyboard shortcuts

I am currently working on a bootstrap dropdown menu that is filled with time zone details. Everything seems to be in order as the dropdown gets populated correctly. However, when I click on it and try to select an item by pressing a key on the keyboard (fo ...

Utilizing transitions to control the visibility of div elements

This is what I have achieved so far: function getOffsetRect(elem) { var box = elem.getBoundingClientRect() var body = document.body var docElem = document.documentElement var scrollTop = window.pageYOffset || docElem.scrollTop || body.sc ...

Eliminate the blank choice from X-editable Select

Lately, I've been dealing with AngularJS and X-editable and one thing that has been troubling me is the fact that the select option always includes an empty first child. Here's the HTML <span editable-select="curuser.roles" e-name="roles" ...

Experience the sleek and intuitive Material Design Lite navigation drawer, inspired by the likes of Google Android

Currently, I am incorporating Material design lite into my website and have found many templates that work well. However, I am looking to make a slight design modification to the navigation drawer. Here is the dashboard template I am currently utilizing: ...

Submit a Bootstrap modal with AJAX and receive blank $_POST data

After setting up my modal form with pre-defined data, I encountered an issue when trying to submit it with ajax. The form.php script was receiving an empty $_POST array even though the submit button was being clicked. What could be causing this problem? & ...

Interactive Bootstrap Modals

Is it possible to create a "page" viewing experience within a bootstrap modal? This type of user interface flow is common in many mobile apps, but after reviewing Bootstrap's components, I'm unsure how to implement this or if it's even supp ...

Tips for replacing the default pointer image

Is there a way to change the default image for cursor: pointer to a custom image without having to create a new class for every element? Creating a class and specifying the hover value for cursor is not an ideal solution as it would require adding the cla ...

Clearing input fields after entering information

Is there a way to automatically assign the value from a scanner input to a variable in an input box and clear it for the next input? HTML <ion-view hide-nav-bar="true"> <ion-content class="padding"><br> <label class="item item ...

Sort data by various categories using Vue.js

I need help filtering my JSON data based on multiple categories using Vuejs, specifically with Vue 3 and collect.js. Here's the template I'm working with: <select v-model="selectedCategory" multiple> <option :value="n ...

Should simple logic be placed in the Container or Presentational component in React?

I am facing a scenario where a container component passes an array of objects to a presentational component for rendering. Within the presentational component, I need to display the count of objects that meet specific criteria. Should I handle this count ...

Searching for multiple array elements based on their values can be achieved by using various techniques

I am trying to find a way to select multiple elements from an array that share the same value. When I use array.find(), it only returns the first element that matches the condition. For example, in the code below, only "Donald Trump" is displayed in the co ...

Update the canvas box's color when you interact with it by clicking inside

I'm in the process of developing a reservation system and I'm looking to implement a feature where the color of a Canvas changes when clicked. The goal is for the color to change back to its original state when clicked again. Snippet from my res ...

Error occurs when attempting to access window.google in Next.js due to a TypeError

I've been working on integrating the Google Sign In feature into my Next app. Here's how I approached it. In _document.js import React from 'react'; import Document, {Html, Head, Main, NextScript } from 'next/document'; expo ...

Error message indicating a prop type warning occurred in an inactive component

I received a warning message for a page that I am not currently visiting (I'm on the https://example.com/this-page). Warning: Failed prop type: The prop `query` is marked as required in `OtherPage`, but its value is `undefined`. "prop-types&quo ...

Ways to incorporate onload animation into a Pie chart with billboard js

I am currently working on implementing a pie chart with animation using billboard js. However, I am facing difficulties in applying the onload animation. Can anyone provide guidance on how to achieve this? For reference, you can view an example of the des ...

Emphasizing the weekends (Saturday and Sunday) on Material UI's date picker

Does anyone know how to make my date picker highlight weekend days (Saturday and Sunday) using the material-UI-datepicker library? I've tried a few methods but so far nothing has worked. Any assistance would be greatly appreciated! ...