Reactjs 此表达式不可调用。类型';Thunk<;集合,未定义,任意,{},任意>';没有呼叫签名
这是我的密码。我想采取行动,上面的错误还在继续Reactjs 此表达式不可调用。类型';Thunk<;集合,未定义,任意,{},任意>';没有呼叫签名,reactjs,react-redux,easy-peasy,Reactjs,React Redux,Easy Peasy,这是我的密码。我想采取行动,上面的错误还在继续 const action = useStoreActions( (actions : StoreModel) => actions.collections.fetchCollections); useEffect( () => { action() },[]) 这是我的collections对象,我想访问fetchCollections。 这是任何简单的状态管理代码。这里我删除了fetchCollectionsRequest
const action = useStoreActions( (actions : StoreModel) => actions.collections.fetchCollections);
useEffect( () => {
action()
},[])
这是我的collections对象,我想访问fetchCollections。
这是任何简单的状态管理代码。这里我删除了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));
}),
}
这是因为您正在键入
useStoreActions
hook的actions
参数(它不直接符合正确的类型)
相反,请使用:
//组件外部
从“easy peasy”导入{createTypedHooks};
const{useStoreActions}=createTypedHooks();
//组件内部:
const{fetchCollections}=useStoreActions(actions=>actions.collections);
问题解决了。谢谢
import { collectionsModel, Collections } from './collectionsModel';
export interface StoreModel {
collections : Collections;;
}
export const model = {
collections : collectionsModel,
}
// outside the component
import { createTypedHooks } from 'easy-peasy';
const { useStoreActions } = createTypedHooks<StoreModel>();
// inside the component:
const { fetchCollections } = useStoreActions(actions => actions.collections);