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

644
Vistas
Reactjs/Nextjs onClick event not working for external component

I have a main page, with a button component i create to reuse in my project. My issue is when i add the onClick event to my external component, the click event is not working, but is i create the same button inside my main page, the click event works just fine

Button Component

import React from "react";
import styled from "styled-components";

const BigButton = (props): JSX.Element => {
    return <>{props.red ? <BigBtn red={props.red}>{props.val}</BigBtn> : <BigBtn>{props.val}</BigBtn>}</>;
};

export default BigButton;

const BigBtn = styled.button`
    font-style: normal;
    font-weight: 500;
    font-size: 12px;
    line-height: 15px;
    color: #f5f5f5;
    width: 78px;
    height: 30px;
    background: ${(props) => (props.red ? "#BD2129" : "#2e3034")};
    border: ${(props) => (props.red ? "initial" : "1px solid #494b4f")};
    border-radius: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
`;

This works on main page

<button onClick={buttonClose}>Close</button>

Button Component on main page - This doesn't work on main page

<BigButton val="Cancel" onClick={handleClose} />

Close function

const handleClose = (e) => {
        e.preventDefault();
        props.onClose();
    };
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your component does not look correct. You do not have button inside the component and onClick event. You should update like this

import React from "react";
import styled from "styled-components";

const BigButton = (props): JSX.Element => {

const handleClick = () => {
            props.onClick()
    }
    return <>{props.red ? 
                    <BigBtn red={props.red}>
              <button onClick={handleClick}>
                  {props.val}
              </button>
            </BigBtn> : 
              <BigBtn>
                <button onClick={handleClick}>
                  {props.val}
                </button>
              </BigBtn>}
           </>
};

export default BigButton;

const BigBtn = styled.button`
    font-style: normal;
    font-weight: 500;
    font-size: 12px;
    line-height: 15px;
    color: #f5f5f5;
    width: 78px;
    height: 30px;
    background: ${(props) => (props.red ? "#BD2129" : "#2e3034")};
    border: ${(props) => (props.red ? "initial" : "1px solid #494b4f")};
    border-radius: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
`;
about 4 years ago · Juan Pablo Isaza Denunciar

0

Are you adding the onClick event listener to your <BigBtn> styled button?

Your BigButton component is receiving a prop called onClick so you should add it as a function that gets fired on a click event on your styled button.

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