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

124
Visualizações
Pass a handler from class to component in loop

I want to pass a click handle (handleDeleteUser) to other component, from user.js to DropDownUserTool.js actually:

User.js

handleDeleteUser = (id) => {
  alert(id);
};
...
// in render
User.data.map(function (User, i) {
  return (
    <DropDownUserTool
      id={User.id}
      click={(e) => {
        this.handleDeleteUser(e);
      }}
    />
  );
});

DropDownUserTool.js

const DropDownUserTool = (props) => {
  return (
    <ButtonDropdown isOpen={dropdownOpen} toggle={toggle}>
      <DropdownToggle color="secondary" caret>
        tools
      </DropdownToggle>
      <DropdownMenu>
        <DropdownItem>
          <Link to={"/User/Edit/" + props.id}>Edit</Link>
        </DropdownItem>
        <DropdownItem onClick={() => props.click(props.id)}>
          Delete
        </DropdownItem>
      </DropdownMenu>
    </ButtonDropdown>
  );
};

But after click it return an error:

TypeError: Cannot read properties of undefined (reading 'handleDeleteUser')

On this line:

<DropDownUserTool id={User.id} click={(e) => {this.handleDeleteUser(e)}}/>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are trying to reach this inside map, just define this ! inside render before loop:

let realthis = this;

Then call your handler like this:

<DropDownUserTool id={User.id} click={(e) => {realthis.handleDeleteUser(e)}}/>
about 4 years ago · Juan Pablo Isaza Relatório

0

When you're doing your map you're using a standard function call and losing your context. Use an arrow function instead.

class User extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [{ id: 1 }, { id: 2 }, { id: 3 }]
    };
  }

  handleDeleteUser = (id) => alert(id);

  render() {
    return (
      <div>
        // this needs to be an arrow function
        // that way you won't change the context of `this`
        {this.state.data.map((item, i) => {
          return (
            <DropDownUserTool
              id={item.id}
              key={item.id}
              handleDelete={this.handleDeleteUser }
            />
          );
        })}
      </div>
    );
  }
}

const DropDownUserTool = ({ handleDelete, id }) => (
  <button onClick={() => handleDelete(id)}>Delete</button>
);
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