Home > Back-end >  When I render a button inside a material-ui table, the button is not clickable
When I render a button inside a material-ui table, the button is not clickable

Time:10-08

My table looks as such:

            <TableContainer component={Paper} style={{height: "40vh", width: "90vh"}}>
                <Table size="small" sx={{ minWidth: 200 }}>
                    <TableHead>
                        <TableRow>
                            <TableCell align="center" width="90"></TableCell>
                            {consequences_description.map((description) => (
                                <TableCell align="center" width="90">{description}</TableCell>
                                )
                            )}
                        </TableRow>
                    </TableHead>
                    <TableBody>    
    
                        {risk_matrix.map((row, row_index) => (
                        <TableRow
                            key={row_index}
                            sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
                        >
                            <TableCell component="th" scope="row">
                                {likelyhood_description[row_index]}
                            </TableCell>
                            {row.map( (column, column_index) => (
                                <TableCell align="center">
                                    <ToggleButton 
                                        risk={column}
                                        row_index={row_index}
                                        column_index={column_index}
                                        />
                                </TableCell>
                            ))}
                        </TableRow>
                        ))}
    
                    </TableBody>
                </Table>
            </TableContainer>

The button inside the table is called <ToggleButton/> and it looks as such:

        <ThemeProvider theme={filtersTheme}>    
          <Button variant="contained" color={handleColor()} onClick={console.log("Clicked")} >{risk}</Button>
        </ThemeProvider>

As soon as the table renders, the console shows "Clicked", meaning the onClick function gets called for the button as soon as it renders inside the table for some reason, but I am not able to click it and fire the onClick function. Is there a way to do this?

CodePudding user response:

Try to call the onClick function like this:

onClick={() => { console.log('Clicked');
  • Related