Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
click handler not toggling state

I'm trying to create a simple app where if you click on a button, a modal overlay appears, and if you click on the 'x' in the modal it disappears. I made a component for my button, called ShowOffer, and I have an onclick on it which toggles the boolean value of modalVisible, which is a piece of state. However, nothing happens when I click on it. I made another button element with the same onclick, and it seems to work fine. Here is a code sandbox

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This is the simple logic. Your ShowOffer component is not identify the onclick event and this component's button is not have any event handlers. So you just pass the event or directly pass the function name for access the event. Passing props name is the important one.

<ShowOffer display={"block"} onClick = {toggleBox}/>
export default function ShowOffer({ display, onClick}) {
  return (
    <button style={{ display: `${display}` }} className="show-offer" onClick={onClick}>
      Show Offer
    </button>
  );
}

or

<ShowOffer display={"block"} toggleBoxFunct = {toggleBox}/>
    export default function ShowOffer({ display, toggleBoxFunct }) {
      return (
        <button style={{ display: `${display}` }} className="show-offer" onClick={toggleBoxFunct}>
          Show Offer
        </button>
      );
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are adding onClick on the ShowOffer component, but here you are just passing it as a prop in it.

<ShowOffer display={"block"} onClick={toggleVisibility} />

is same as

React.createElement(ShowOffer, {
  display: "block",
  onClick: toggleVisibility
});

Under the hook, you are just passing an argument to a function

You have to add onClick event on the button in ShowOffer component as:

Live Demo

Codesandbox Demo

<button
      style={{ display: `${display}` }}
      onClick={toggleVisibility}
      className="show-offer"
    >
      Show Offer
    </button>

and you have to pass the toggleVisibility callback to ShowOffer as:

<ShowOffer display={"block"} toggleVisibility={toggleVisibility} />
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use concept of Callbacks,
App.js, make following changes,
pass toggleVisibility={toggleVisibility} as props, no need to mention onClick at component but at button

  return (
    <div className="App">
      <Modal display={modalVisible ? "flex" : "none"} />
      <ShowOffer display={"block"} onClick={toggleVisibility} toggleVisibility={toggleVisibility}/>
      <button onClick={toggleVisibility}>Button</button>
    </div>
  );

ShowOffer.js, props passed function, call that function here with onclick,

import React from "react";
import "./ShowOffer.css";

export default function ShowOffer({ display, toggleVisibility }) {
  return (
    <button style={{ display: `${display}` }} onClick={toggleVisibility} className="show-offer">
      Show Offer
    </button>
  );
}

working solution is here, https://codesandbox.io/embed/modal-overlay-tnis9?fontsize=14&hidenavigation=1&theme=dark

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda