Clicking on the inner part of the Material Table will display only the value of

I'm currently facing an issue with a custom rendered column that contains a menu button. When I click on the menu button, it opens up a menu like this:

https://i.stack.imgur.com/4Lxd5.png

Now let's take a look at the code snippet below:


columns={[
    {
        title: 'Actions',
        field: 'tableData.id',
        render: rowData => {
            return (
                <div>
                    <IconButton aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick}>
                        <MenuIcon />
                    </IconButton>
                    <Menu
                        className={classes.menu}
                        id="simple-menu"
                        anchorEl={anchorEl}
                        keepMounted
                        open={Boolean(anchorEl)}
                        onClose={handleClose}
                    >
                        <MenuItem className={classes.sendMail} onClick={() => { console.log(rowData) }}>
                            <ListItemIcon className={classes.icon}>
                                <SendIcon fontSize="default" />
                            </ListItemIcon>
                            Send Assessment Email
                        </MenuItem>
                    </Menu>
                </div>
            )
        },
    },

The issue I'm facing is that when I click on the IconButton component, the values from the rows (rowData) are correct. However, when I click on the MenuItem, it only shows the last rowData value regardless of which row I select. Any assistance would be greatly appreciated. Thank you.

EDIT: I temporarily fixed this by storing the selected row in useState and using that value for other actions. I'm curious to know if there's a native or default solution for this rather than the workaround I used. I tried preventing event propagation but it didn't work as expected.

Answer №1

Thank you for reaching out with your question and letting me know I'm not alone...

While I can't guarantee this will solve the issue, here is a workaround that has been effective for me.

  1. To handle the opening of the menu, I utilize the onClick event to set the selected menu row in the component's state.
    const [slcRow, setSlcRow] = React.useState(null);
    const handleClick = (event, row) => {
        setAnchorEl(event.currentTarget);
        setSlcRow(row);
    };

    <Button 
        aria-controls="simple-menu" 
        aria-haspopup="true" 
        onClick={(event ) => {handleClick(event, row) }}
        >
        Open Menu
    </Button>
  1. When clicking on a MenuItem, I retrieve the selected row from the state and execute necessary actions based on it.
   const handleEditOpen = (event) => {
       let row = slcRow;
       loadEditData(row.clientId);
       setEdit(true);
       setAnchorEl(null);
   };

   <Menu id="long-menu" anchorEl={anchorEl} keepMounted open={open} onClose={handleClose1}>
       <MenuItem onClick={(event) => { handleEditOpen(event) }}>Edit </MenuItem>
   </Menu

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

Getting rid of a cookie from the header in Reactjs

Currently, I'm developing an application with Reactjs and utilizing the Next.js framework. To manage user authentication, I've implemented cookies. However, I am facing a challenge when it comes to logging out users and removing the cookie. Could ...

Express.js: app.post is failing to establish a connection

Currently, I am in the process of developing an application using NodeJS and have encountered a roadblock. When testing the post request with Postman, the server does not seem to enter the function. However, GET requests function as expected. Do you have a ...

Error encountered: "npm ERROR! Unable to retrieve property '0' when using npm version 5.8.0"

When I tried to update my React/Typescript application with the beta version of material-ui using the command npm install material-ui@next, I encountered an error message: npm ERR! Cannot read property '0' of undefined npm ERR! A complete log o ...

React JS does not allow TextField and Select to change

I am relatively new to full stack development and I am currently working on a project to enhance my understanding of frontend development with React JS. While working on this project, I have been using Redux without any issues so far. However, I am facing ...

Next.js and Material UI - issues with dynamic styles not functioning

I recently started using Next JS in combination with Material UI, following the example project setup provided in the documentation on Github: https://github.com/mui-org/material-ui/tree/master/examples/nextjs My main objective is to utilize Material UI&a ...

Notify user when Redux action is completes in ReactJS/Redux application

I've been working on a custom notification component that should display a message and then hide itself after a few seconds when certain redux actions, like login_fail, are triggered. However, I'm facing an issue where the message briefly pops up ...

Utilizing React JSX within HTML structures

Attempting to integrate React into a basic HTML file, I came across an official guide on this website. However, I am struggling to grasp how to render a JSX-encoded component and how to nest one component within another: class LikeButton extends React.Co ...

Creating a flexible route path with additional query parameters

I am facing a challenge in creating a unique URL, similar to this format: http://localhost:3000/boarding-school/delhi-ncr However, when using router.push(), the dynamic URL is being duplicated like so: http://localhost:3000/boarding-school/boarding-school ...

Webpack focuses solely on serving HTML files, neglecting to deliver the underlying code

Hey there! I'm currently working on a project that involves using React, TypeScript, and Webpack. I ran into some errors previously that prevented the code from compiling, but now that's fixed. However, when I run webpack, it only serves the HTML ...

Utilizing Google's ReCaptcha v3 with Next.js

Trying to integrate react-google-recaptcha-v3 into my React application using version 16.13.1 & Next version 9.4.4. Encountering an issue at const result = await executeRecaptcha("homepage") in pages/index.js. console.log(process.env.GOOGLE ...

Creating a React component that manages its own UI state without any external

I'm currently working on creating a select input component using React. The select itself should be a simple UI component without much functionality, but it does need to maintain its own state to determine whether or not to display the options list. ...

Dealing with Unauthorized Errors (401) in an Axios React-Redux Application

I'm looking to handle the 401 unauthorized error that my server may output by dispatching an action. I've noticed many people using axios interceptors for this purpose. Could someone explain what interceptors are and guide me through implementing ...

Leveraging Algolia's React library, the CurrentRefinements component allows for real-time

Hello there! I'm in the process of creating a customized CurrentRefinements component, and everything is going smoothly except for one issue with the refine() function. Here's the code snippet for my component: import React from 'react' ...

Error encountered: The module '@mui/x-data-grid' does not export 'GridActionsCellItem'

I'm facing an issue while trying to import 'GridActionsCellItem' from '@mui/x-data-grid'. Here's the code: import { GridActionsCellItem } from '@mui/x-data-grid'; An error message pops up indicating: Attempted impor ...

Forward checkout.session items from Stripe webhook to Supabase

Currently, I am utilizing next.js in conjunction with Stripe webhooks to insert checkout sessions into Supabase for generating a customer's order history. While I have successfully managed to store the entire order information in a table named 'o ...

Seeking an improved approach to invoking a React component based on varying states. Open to suggestions for a more effective

I am currently working with a Modal Component from Material UI and my goal is to conditionally render this component based on the state provided by useState. This is what I have done so far - <ModalPopup open={deleteRole} onClose={handleDeleteClo ...

How do I incorporate scrolling into Material-UI Tabs?

I am currently incorporating Material-ui Tablist into my AppBar component. However, I am facing an issue with the responsiveness of the tabs. When there are too many tabs, some of them become hidden on smaller screens. For more information on the componen ...

Performing asynchronous operations in React with axios using loops

On the backend, I have a socket set up to handle continuous requests from the server. Now, my goal is to send requests to the backend API continuously until a stop button is clicked. Using a loop for this task is considered bad practice as it never checks ...

After updating to version 11 of Next.js, encountered an error due to an unsupported file type: undefined

While attempting to load an image after updating Next.js to version 11 using the code below: import segmentLogoWhitePng from 'assets/images/my-image.png' I encountered the following error message: TypeError: unsupported file type: undefined (fil ...

Easiest method for creating a distinct identifier for a form-inputted value in React

My goal is to extract the value from the input field, generate a unique id for it, and then pass both the name and id to the addFolder() function. The object will then be added to the end of the state's folders array. My main challenge lies in creatin ...