Search code examples
javascriptreactjsreact-nativecallbackarrow-functions

How to use callback function with parameters?


I have a function, that is returning callback. But meetUid is static on here.

fetchMeetDetails = async (callback) => {

        const meetUid = '1a712f91-974d-4185-9389-f7b1b4edede2';
        const snapshot = await database().ref(`/meets/${meetUid}`).once('value');

        callback(snapshot.val())

    }

I want to get meetUid from parameters. like that fetchMeetDetails = async (callback,meetUid) => { but I cannot do it. Because we got an error (TypeError : callback is not a function). How can I use this function with callback and parameters ?


Solution

  • TypeError : callback is not a function
    Type is the key word here.

    Compiler takes the callback parameter as anything because of how you declared it first.
    Not a problem.

    Next, compiler meets this callback(snapshot.val()) and then it gets confused.
    Then it says, "Well, am confused with this type. Let me complain!"

    In such situation, the compiler wants to know the default type of that parameter.

    // Since here "callback" is a function, pass "callback" as a default function
    
    (callback=f=>f, ...rest) => {}
    

    It's the same concept when passing "props" to "children" in react.