I have a code like below
Here I want to call the function expandUnexpandSchema from the callback function expandSchema. How this can be done? I could be able to call the loadSchemaInfo callback function from expandSchema, but that is causing all the code to execute. But I want only the particular block to be executed.
   function useSchemaState() {
const [expand, setExpand] = useKeyState('expand', false);
const expandSchema = useCallback(() => {
    setExpand((expand) => !expand);
    loadSchemaInfo();
}, [setExpand]);
const loadSchemaInfo = useCallback(
    async(connectionId, reload) => {
        // Some more lines of code
        const expanded = {};
        expandUnexpandSchema() {
            if (data) {
                Object.keys(data).forEach((schemaName) => {
                    expanded[schemaName] = expand;
                });
            };
        }
    );
};
Thanks in advance guys.
Update:
if (data) // the data here is retrieved by calling REST API, which will take lot of time if were to retrieve again. If defining expandUnexpandSchema outside of loadSchemaInfo , is there any way to retain the data by storing it in a variable with out losing it?
 
     
    