Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
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 answers
Answer question

0

Have you tried:

foo.getInstance().fooCallback = this.props.argumentFunc.bind(this)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!