Functions as parameters

Let's say we have a method that is used in more than one place, but should result in a different final action taking place depending on where it is called from. For example, the getForm() method for editing a web advert. When this is called from the company record, the specified company tab should reload after the advert has been saved. This is defined in a reloadTab(string) method. If getForm() is called within the current bookings report, the final action is defined as getResults() (no parameters).

Ideally, you want to pass the function to be called into the getForm method as a parameter, but how do you define the parameter? Answer: as a zero argument function that returns void (() => void):

export const getForm = (action: () => void) => {
    // do stuff
    action;
}

So how do you pass in the getResults and reloadTab functions?

The getResults function is easy because it happens to match () => void. You simply pass in the name of the function (no parentheses):

getForm (getResults);

The reloadTab function takes a string as a parameter ((tab:string) => void) so it needs to be wrapped in a zero-argument function that calls reloadTab with any arguments:

getForm (() => reloadTab(tab));
Last updated: 9/3/2025 12:48:10 PM

Latest Updates

© 0 - 2025 - Mike Brind.
All rights reserved.
Contact me at Mike dot Brind at Outlook.com