This expression is not callable. Type 'Thunk<Collections, undefined, any, {}, any>' has no call signatures

749 Views Asked by At

Here is my code. I want to call action and above error is keep coming.

const action = useStoreActions( (actions : StoreModel) => actions.collections.fetchCollections);

useEffect( () => {
    action()
},[])

This is my collections object and I wants to access fetchCollections. This is any easy-peasy react state management code. P.S: Here I removed fetchCollectionsRequest, fetchCollectionsSuccess, fetchCollectionsFailure.

import { Action, action, thunk, Thunk } from 'easy-peasy';

export interface Collections {
    isLoading : boolean;
    collections : any[];
    error : string;
    fetchCollections : Thunk<Collections>;
}

export const collectionsModel : Collections = {
    isLoading : false,
    collections : [],
    error : '',
   
    fetchCollections : thunk( actions => {
        actions.fetchCollectionsRequest();
        axios.get('/categories?filter={"where":{"isHidden":false}}')
            .then( res => actions.fetchCollectionsSuccess(res.data))
            .catch( err => actions.fetchCollectionsFailure(err.message));
    }),
}

And this is my model.tsx

import { collectionsModel, Collections } from './collectionsModel';

export interface StoreModel {
    collections : Collections;;
}

export const model = {
    collections : collectionsModel,
}
1

There are 1 best solutions below

0
On BEST ANSWER

This is because you are typing the actions parameter of the useStoreActions hook (which does not directly conform to the correct types).

Instead, use typed hooks:

// outside the component
import { createTypedHooks } from 'easy-peasy';

const { useStoreActions } = createTypedHooks<StoreModel>();

// inside the component:
const { fetchCollections } = useStoreActions(actions => actions.collections);