score:0
Track the selected row id for displaying the action panel.
import "./styles.css";
import TableCell from "@mui/material/TableCell";
import { useState } from "react";
import { Button } from "@mui/material";
const data = [
{
id: 1001,
label: "Action1"
},
{
id: 1002,
label: "Action2"
}
];
export default function App() {
const [selectedId, setSelectedId] = useState();
const handleClick = (id) => {
setSelectedId(id !== selectedId ? id : null);
};
return (
<>
{data.map((val) => (
<div>
<TableCell>
<Button
className="active-select-option"
onClick={() => handleClick(val.id)}
>
{val.label}
</Button>
{selectedId === val.id && (
<div className="select-option">
<ul id="action1" className="select-option-inner rounded">
<li>Buy More</li>
<li>Sell </li>
<li>Next Li Tag</li>
<li>Edit Detials</li>
</ul>
</div>
)}
</TableCell>
</div>
))}
</>
);
}
Demo:- https://codesandbox.io/s/lucid-https-jkp84?file=/src/App.js:0-1114
score:0
The Problem is you're not passing e in handleClick. A way to do that is passing handleClick via Anonymous arrow function, so that you can reference it instead of invoking it.
<Button className = 'active-select-option'
onClick = {()=>handleClick(e)}>Actions</Button>
score:0
I don't see any issues with your code but you can simplify a bit the handleClick
method (there's no need to handle the e.preventDefault()
) and also use the &&
condition to display the <div>
.
If list
is true
the <div>
will be displayed, otherwise false
is returned and the <div>
is not displayed.
You can avoid to explicitly use the list
variable with setList(!list)
and instead use the value of the previous state provided as argument of the inner function of setList(l => !l)
If it's not going to work, could id be a css issue?
const [list, setList] = useState(false)
const handleClick = () => {
setList(l => !l);
}
return (
<TableCell>
<Button className = 'active-select-option'
onClick = {handleClick}>Actions</Button>
{list &&
<div className = "select-option">
<ul id = 'action1' className = "select-option-inner rounded">
<li>Buy More</li>
<li>Sell </li>
<li>Next Li Tag</li>
<li>Edit Detials</li>
</ul>
</div>}
</TableCell> )
Source: stackoverflow.com
Related Query
- Open a div element on a button click with ReactJS
- How to render DIV element with a button click on ReactJs
- ReactJS Ant Design - Open DatePicker on button click
- How to get key attribute of parent div when button is click in ReactJS
- ReactJS + Redux: How to trigger a button to open up a Mac Mail/Windows Mail with property email?
- How to open an <input type="file"/> with a button click in React
- Is there any way we can export the Antd table data into an excel sheet on button click with reactjs
- How to open a new page with specific dataset with a button click in react?
- when i click on update button i want the course open in add course with the same values
- Handle button click with two different handler in different condition - ReactJS
- reactjs state is not being updated with button click using useState hook
- How to make open url on click on button in reactjs
- Can I open a jw modal (imported from jw-react-modal) in reactjs without button click
- move between two div with tow button using reactjs
- create and open a pdf on a click of link and show print button with react-pdf
- ReactJS showing div element on click
- Changing div colour styling with button click in React
- Add form element on every button click with react
- How can I toggle a FormControl from disabled to enabled with a button click in Reactjs
- Reactjs click button handler with a parameter within a table parameter is always empty
- Passing a ref to component props then focus on input element on div click with React.createElement
- reactjs scroll to bottom with user defined speed when user click start button
- How to use array method to change different color on div in. In single button reactjs click
- In API call If I click Submit button open 200code Success page other wise open Error Page 404 In ReactJS
- Deactivate input in react with a button click
- How to open a page in new tab on click of a button in react? I want to send some data to that page also
- react open file browser on click a div
- tooltip div with ReactJS
- 'A valid React element (or null) must be returned' error with one of the component in ReactJS
- Submit form in ReactJS using BUTTON element
More Query from same tag
- What might cause an MUI Snackbar to not generate the correct class names?
- Change color of dataLables in ApexChart in React.js
- How can i achieve 100% reusability of that react-bootstrap component?
- Multiple Bootstrap React components in one Bootstrap Row
- Styled components arithmetic operations
- DOM Event doesn't working without arrow function
- How to increment simple counter and counter with object component using redux
- How can I call a component's method from another component in React?
- foreach inside mapping and render data
- How to link Django and React URLs to perform actions?
- How to use react-select-async-paginate library on change of different select?
- javascripts default replace function not working in nextjs
- Using async/await inside a React functional component
- Are react stateless components equal to ReacDOM.PureComponents
- React / Redux / Reselect - is mapped state to props using selectors synchronous or asynchronous?
- Set <select> default value with redux-form in React
- Try to hide header on login page but did not work
- Reset scroll position on component re-render
- gatsbys onClickOut is not defined
- Hover Effect in React is too slow (state update for hover effects)
- Iterate over children, find last element
- devDependencies not installing
- How to fix false action dispatching
- how can i use global style when develop component library using styled-components
- Customizing Hubspot form error message (using react.js)
- How to pass variables in Route which has dynamic URL in ReactJS?
- How to make react select an params on axios?
- I'm using React-Bootstrap. The e.preventDefault() is not working
- Add new component dynamically
- Pass Image path from JSON in react