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

120
Visualizações
I am trying to pass a function as a prop to my component

I have an Activity.js Component that I am bringing in to my App.js component that renders activities. I would like add an onlick event to a button in this component by passing a function as a prop to that button, for some reason the button isnt working, right now its a simple console.log for the logic.

The activities do display when I map through the array of objects but it seems the button isn't taking the props. Can somebody help me understand my why deleteActivity function doesn't run when I click the button?

Acitivity.js component


import React, {Component} from 'react'



export default function Activity(props) {
    return (
      <div>
        <h1>Things I like to do </h1>
        {props.activities.map((activity) => (
          <div key={activity.activity} style={{ display: "flex", justifyContent: "space-between" }}>
            <li >{activity.activity}</li>
            <button onClick={() => props.deleteActivity}>X</button>
          </div>
        ))}
      </div>
    );
  }

App.js Component


import React, { Component } from 'react';
import ListContacts from './ListContacts';
import Activity from './Activity'

class App extends Component {
state = {
activities :  [
  {
    id: 1,
    activity: "pool"
  },

  {
    id: 2,
    activity: "games"
  },

  {
    id: 3,
    activity: "sports"
  }
]

}

deleteActivity = ()=>{
  console.log("hello")
}


 render() {
    return (
      <div>

       <Activity activities={this.state.activities} deleteActivity={this.deleteActivity} />
      </div>
    )
  }}

export default App;

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

0

Youre passing the prop correctly, but you arent actually running the function you passsed. instead of () => { props.deleteActivity }, try adding () to the end of your function. example: () => { props.deleteActivity() }

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