Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Open a div element on a button click with ReactJS

I am using a map function inside the render function and upon a button click want to display the following div but I am unable to do. Earlier the onClick was triggered for the all the buttons inside the table of each row which ideally should not be the case.

Code :

const [list, setList] = useState(false)

 const handleClick = (e) => {
        e.preventDefault()
        if(e.target.value) {
            setList(!list)
        }
        setList(!list)
    }

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> )
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

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>
about 4 years ago · Juan Pablo Isaza Relatório

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> )
about 4 years ago · Juan Pablo Isaza Relatório

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda