Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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 Respostas
Responde à pergunta

0

Have you tried:

foo.getInstance().fooCallback = this.props.argumentFunc.bind(this)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda