Skip to content
Advertisement

a dispatch that im trying to push into an array is calling it instead

I have a simple custom hook with a few dispatches and callbacks on them.

It looks something like this:

const { updateText, updateImage, updateTitle, batchJobs } = useBlock();

This is called a larger component that props open a big edit window. Upon making those changes, I click my confirmation button that does something similar to this:

   const onConfirm = () => {
        let toDispatch = [];
        // some logic
        toDispatch.push(updateText(id, text)); // this gets called right away
        console.log(toDispatch); // this is undefined or null
        batchEdit(toDispatch);
    }

It seems that my updateText dispatch is being called right when I try to push it, as it does do its job. But that is not what I’m trying to achieve.

Within batchEdit I want to loop through all the function and call them, then upon completion I call a callback that updates the screen. I can never get into to it as the array is always becoming undefined.

Advertisement

Answer

The way you want to pass your function does not give it, but only adds the value returned by the function to the array. To pass function ( not returned value ) you should create callback.

toDispatch.push(() => updateText(1, "sss"))
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement