Switch between various components using multiple buttons in Next.js

I am in the process of creating a component that will display different components depending on which button the user selects.

Here are the available “pages”:

`

    const navigation = [
      { name: "EOQ", return: "<EOQgraph />" },
      { name: "ROP", href: "<ROPgraph />" },
      { name: "Table", href: "<Table />" },
      { name: "ROC+SS", href: "<ROCSSgraph />" },
      { name: "Table+SS", href: "<TableSS />" },
    ]

`

The function and UseState that should receive the key (not all components are implemented yet) are as follows:

`

  const [toggle, setToggle] = useState('')

const wantedGraph = (value) => {

    switch (value){
      case  "EOQ":
        return setToggle(<EOQgraph />);
      case  "ROP":
        return setToggle(<ROPgraph />);

`

And the return statement uses a .map to render all the buttons: `

        return(
            <div>
                {navigation.map((item) => (
           <button type="submit" 
                   className={'btn btn-outline-dark btn-md'} 
                   key={item.name}
                   onClick={() => setToggle(wantedGraph(item.name))}
                   >
               {item.name}
           </button>
            ))}
                <div>
                  { toggle }
                </div>
            </div>
        )

`

Currently, nothing is displaying within the component.

The page functions correctly and the buttons are present. UseState is functioning properly; however, when a button is clicked, the component does not render.

I would like to mention that I am also utilizing Graph.js with each component located on a separate page and imported accordingly.

Answer №1

The current issue arises from the fact that you are assigning the value of toggle to undefined within the onClick function of your button. This occurs because the wantedGraph function retrieves the value returned by setToggle, which does not return anything, hence resulting in undefined.

To resolve this, you have two options:

const wantedGraph = (value) => {
    switch (value) {
      case "EOQ":
        return <EOQgraph />
      case "ROP":
        return <ROPgraph />
    }
  }

You can either modify the wantedGraph function to return the desired component, as shown above, or adjust the onClick handler to exclude calling setToggle:

return (
    <div>
      {navigation.map((item) => (
        <button type="submit"
          className={'btn btn-outline-dark btn-md'}
          key={item.name}
          onClick={() => wantedGraph(item.name)}
        >
          {item.name}
        </button>
      ))}
      <div style={{ color: 'white' }}>
        {toggle}
      </div>
    </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

The functionality for selecting text in multiple dropdown menus using the .on method is currently limited to the first dropdown

Having trouble selecting an li element from a Bootstrap dropdown and displaying it in the dropdown box? I'm facing an issue where only the text from the first dropdown changes and the event for the second dropdown doesn't work. <div class="dr ...

Is there a way to use Jquery to determine if a parent div contains a scroll

I'm trying to find a jQuery example that checks if any parent div has a scroll bar, but I can't seem to locate a helpful one. Here is the code snippet I am working with: <div> <div class="heading"> <div class="visit ...

Run JavaScript code when the HTML file has been modified for the second time

After browsing through this online forum, I stumbled upon a helpful solution. See the code snippet below for reference. However, I encountered a problem in my case. The script in question is essentially monitoring itself. The code resides in index.html an ...

What is the best way to eliminate specific duplicate characters from a string using JavaScript?

I have a project involving managing email responses, where the reply function includes pre-written content like Re: ${Subject of the email} The issue I'm facing is that after the 2nd reply, there is a repeated Re: , so I created a function to remove ...

Maintain synchrony of the state with swiftly unfolding occurrences

I developed a custom hook to keep track of a state variable that increments based on the number of socket events received. However, when I tested by sending 10 simultaneous events, the total value of the state variable ended up being 6, 7, or 8 instead of ...

The amazing magnific popup boasts a pair of close buttons

I recently started working on integrating a front-end HTML theme with a back-end Laravel app. Oddly enough, I noticed that the popup modal is displaying two close x buttons instead of just one. Despite my efforts, I have been unable to pinpoint what exactl ...

Web fonts are functioning properly only on Chrome and Safari for Macintosh operating systems

A web designer provided me with some fonts to use on a website. Below is the content of my fonts.css file: /* Font Awesome */ /* Font Awesome Bold */ @font-face{ font-family: 'font-awesome'; src: url('fonts/font-awesome-bold-webf ...

Incrementing pages, starting with page1.html and continuing to page2.html, page3.html, and beyond, for use with the window

How should I handle this situation? My goal is to automatically navigate to the next page once all necessary functions have been completed. For instance, after all functions on page1.html are finished, it will trigger a function called next_page(). The n ...

Displaying the information fetched using axios on the screen using node.js

Is there a way to display the information from the input boxes in the image on the screen using node js? ...

Align image within box using Bootstrap

My project screenshot is displayed below. The red line separates the current state from the desired outcome. I am struggling to make it fullscreen, meaning it should extend all the way to the corners. Despite trying various methods with Bootstrap 4, I have ...

Troubleshooting: Issue with Button Layout in Ionic's ItemSliding Component

How can I create a list item that allows swiping to reveal an archive button? The requirement is for the icon to appear to the left of the text. I'm referring to the documentation on Button Layout: https://ionicframework.com/docs/api/components/item ...

What is the best way to make a linear gradient that spans the entire vertical space that is visible (or shifts to a solid color)?

I'm trying to figure out how to apply a linear gradient to the body element of my webpage. Here's the CSS code I've been using: body { background: linear-gradient(0deg, white, lightgray); } The issue I'm facing is that the gradien ...

Having trouble finding the element during Selenium JavaScript testing

I need help testing a JavaScript script using Selenium, but I am running into an issue. I cannot seem to find a specific element that I want to click on. Here is a snippet of my JS code where I am trying to click on the Shipping option. I have tried us ...

jQuery Issue - Clash between multiple menus sharing the same class

Hey there! I'm currently diving into the world of jQuery and encountering an issue with my menu. Whenever I click on a menu item with either the "tm-nav-vertical" or "tm-nav-horizontal" class, it removes the .active class from the initial menu item. I ...

Update in slide height to make slider responsive

My project involves a list with text and images for each item: <div class="slider"> <ul> <li> <div class="txt"><p>First slogan</p></div> <div class="img"><img src="http://placehold.it/80 ...

What's the issue with the addClass function not working as expected?

I am currently integrating the website's navbar using PHP, which is why I am unable to use class="active" to highlight the active tab on my navigation bar individually. To solve this issue, I attempted to add the active class to the corresponding tab ...

show information on a separate screen following the selection of a choice

After selecting an option, how can I make #formid appear where the form is filled in with the stock selected in the option section? <div class="form-group"> <label for="exampleFormControlSelect1">Item Name</label> <select clas ...

Live Server in VS Code not refreshing automatically as expected

My problem is with VS Code and Live Server for HTML CSS. I expected the page to automatically reload after editing my HTML, but that's not happening. Even though I have Auto-Save enabled, Live Server isn't updating or refreshing my page automati ...

What is the best way to align a table (datatable) to the left?

How can I align the table to the left without using float:left in the following code? Applying text-align: left to the parent element of the table doesn't seem to work as expected. http://jsfiddle.net/william/B4qJG/1 HTML: <div class="valueList" ...

Assign local variable in Mongoose using query results

I created a function to query MongoDB for a credential (simplified): var query = Credential.findOne({token: token}); return query.exec(function (err, credential) { if (err) return null; return credential; }); The next step is to use this credent ...