【问题标题】:This expression is not callable. Type 'Thunk<Collections, undefined, any, {}, any>' has no call signatures此表达式不可调用。类型 'Thunk<Collections, undefined, any, {}, any>' 没有调用签名
【发布时间】:2020-12-10 05:42:23
【问题描述】:

这是我的代码。我想采取行动,但上述错误不断出现。

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

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

这是我的集合对象,我想访问 fetchCollections。 这是任何简单的反应状态管理代码。 P.S:这里我删除了 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));
    }),
}

这是我的model.tsx

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

export interface StoreModel {
    collections : Collections;;
}

export const model = {
    collections : collectionsModel,
}

【问题讨论】:

    标签: reactjs react-redux easy-peasy


    【解决方案1】:

    这是因为您正在键入 useStoreActions 钩子的 actions 参数(它不直接符合正确的类型)。

    改为使用typed hooks:

    // outside the component
    import { createTypedHooks } from 'easy-peasy';
    
    const { useStoreActions } = createTypedHooks<StoreModel>();
    
    // inside the component:
    const { fetchCollections } = useStoreActions(actions => actions.collections);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 2020-08-08
      • 2020-12-27
      • 2021-04-27
      • 2020-06-13
      相关资源
      最近更新 更多