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

225
Vistas
pass dispatch function to another instance property as callback to execute later on

Sorry for pseudo-code. Think that the problem can be understood like so. Working in a reproducible example.

Consider the following pseudo code:

Class foo:

let fooInstance = null
class Foo() {
    constructor(){
        fooInstance = this;
        this.fooCallback = null // property to assign callback
    }

    static getInstance() {
        return fooInstance
    }

    callbackExecutor() {
        if (this.fooCallback)  // callback always null here even the assignment is ok in the component
            this.fooCallback();
    }
}

React-redux component:

import { argumentFunc } from 'src/argumentFunc'
class MyComponent extends Component {
    constructor(props) { 
        ...
    }
    componentDidUpdate(prevProps, prevState) {
        const foo = Foo.getInstance()
        if (whateverCond) {
            // callback assignment to instance. this.props.argumentFunc() works if called here
            foo.fooCallback = this.props.argumentFunc(); 
        }
    }
}


...
const mapDispatchToProps = (dispatch) => ({
    argumentFunc: () => dispatch(argumentFunc()),
})

export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)

I assign the callback to the instance, but then in the callbackExecutor, this.fooCallback is null.

I tried:
foo = this.props.argumentFunc();
foo = this.props.argumentFunc;
foo = () => this.props.argumentFunc();
foo = () => this.props.argumentFunc;

How can I pass a callback to the instance (fooInstance) to be called later on within the class instance methods(callbackExecutor())?

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

0

Have you tried:

foo.getInstance().fooCallback = this.props.argumentFunc.bind(this)
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