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

259
Vistas
onClick setState is not a function

First of all, I've had a look at other answers and that didn't help.

I have a modal that is passed in a onClose function prop. When the cancel button is clicked it should trigger the onClose function which basically sets the showModal state to false. However, upon clicking the button I get an error that onClose is not a function.

I have checked the type of onClose prop and its coming back as an object. I don't know why.

This is how I am calling the Modal from the parent.

<Modal
   onClose={() => this.setState((prevState) => ({
      showModal: {
         ...prevState.showModal,
         confirmation: false,
        },
    }))}

/>

and this is what the modal looks like.

const Modal = (onClose) => {
  return (
    <div className="Background" >
      <div className="Container">
        <div className="Title">Title of the modal</div>
        <div className="Body">Body of the modal</div>
        <div className="Footer">
          <button type="button" onClick={() => onClose()}>Cancel</button>
        </div>
      </div>
    </div>
  );
};

This is the error from console.

enter image description here

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

0

change this line

const Modal = (onClose) => {

to

const Modal = ({onClose}) => {
about 4 years ago · Juan Pablo Isaza Denunciar

0

//be sure this is being used in a class component! (not a functional) 
export YourClassComponent extends React.ClassComponent {
    constructor(props) {
      super(props);
      this.onCloseModal = this.onCloseModal.bind(this);
    }

    onCloseModal = () => {
      this.setState((prevState) => ({ 
        showModal: {
          ...prevState.showModal,
          confirmation: false,
      }}))
    };

    render() {
      <Modal onClose={this.onCloseModal} />
    };
};
    // In you Modal.jsx, see the prop is setted directly no need to create a new ref to call it
const Modal = ({ onClose }) => {
  return (
    <div className="Background" >
      <div className="Container">
        <div className="Title">Title of the modal</div>
        <div className="Body">Body of the modal</div>
        <div className="Footer">
          <button type="button" onClick={onClose}>Cancel</button>
        </div>
      </div>
    </div>
  );
};

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