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

133
Vistas
Why do setState and useState not run inside event listener callback

I have come across a very weird problem. I have a function that is being used as a callback for an eventListener. This callback has a setState call inside it.

The weird thing is that if I pass the function as a prop to another component and call it in the onClick handler, the setState call executes and its callback prints to the console.

When I'm using it as the callback to the eventListener, on the event being fired, the function is running, but the setState inside it does not run!

No error or warning is thrown on the console.

Code:

Utils.js

import { EventEmitter } from 'fbemitter';

const emitter = new EventEmitter();

CurrentFile.jsx

import { emitter } from './Utils';

class ComponentWithListener extends React.Component {
  state = {
    selections: this.props.selections
  };

  componentDidMount() {
    emitter.addListener('resetSelections', () => this.resetSelections());
  }

  componentWillUnMount() {
    emitter.removeAllListeners('resetSelections');
  }

  resetSelections = () => {
    console.log('resetSelections() called');

    this.setState({ selections: this.props.initialSelections }, () => {
      console.log('successfully reset selections');
      someFunctionCall();
    });
  }
}

OtherFile.jsx

import { emitter } from './Utils';

class OtherFile extends React.Component {
  // ..state declaration and other functions
  
  resetStoredFilters = () => {
    // do some things
    emitter.emit('resetSelections');
  }

  // other functions and render()
}

So here, whenever resetStoredFilter() is run, the setState inside resetSelections() is not run, and the output I get on console for resetSelections() is:

resetSelections() called

someFunctionCall() runs.

Compare this to when I pass resetSelections as a prop to a child component and call it from inside an onClick handler from inside the child component:

// body of some function inside child component
onClick={resetSelections}

When this element is clicked inside the child component, the output I get on the console for resetSelection() is:

resetSelections() called
successfully reset selections

someFunctionCall() does not run.

I even tried using functional components and useState hook in CurrentFile.jsx to check if that makes a difference. I see the same issue still.

Why does this happen? And how to make the setState run in eventListener callbacks?

about 4 years ago · Juan Pablo Isaza
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