gpt4 book ai didi

reactjs - 如何解决 typescript Redux 连接错误 ActionCreatorsMapObject

转载 作者:搜寻专家 更新时间:2023-10-30 21:03:42 25 4
gpt4 key购买 nike

typescript 有错误

ERROR in [at-loader] ./src/app/components/profile/userProfile/_userMain.tsx:130:37 
TS2345: Argument of type 'typeof "/usr/src/app/src/js/src/app/redux/actions/sessionActions"' is not assignable to parameter of type 'ActionCreatorsMapObject'.

ERROR in [at-loader] ./src/app/components/profile/userProfile/_userMain.tsx:133:124
TS2345: Argument of type 'typeof UserProfileMain' is not assignable to parameter of type 'ComponentClass<(state: any) => any> | StatelessComponent<(state: any) => any>'.
Type 'typeof UserProfileMain' is not assignable to type 'StatelessComponent<(state: any) => any>'.
Type 'typeof UserProfileMain' provides no match for the signature '(props: ((state: any) => any) & { children?: ReactNode; }, context?: any): ReactElement<any>'

是否可以在没有装饰器的情况下解决这个问题? (@连接)

interface IActions {
avatarUpload: (file:any) => void;
}

export interface IMainProps {
auth: number,
actions: IActions
}

class UserProfileMain extends React.Component<IMainProps, any> {
constructor(props:any){
super(props);
this.state ={
files: [],
}
}

render(){
return(<div />)
}
}

const mapStateToProps = (state:any):any => {
return {
auth: state.authStatus.data.user.id,
};
};

const mapDispatchToProps = (dispatch:any):any => {
return {
actions: bindActionCreators(sessionActions, dispatch) //First error (130:37)
};
}
export default connect<typeof mapStateToProps, typeof mapDispatchToProps, IMainProps>(mapStateToProps, mapDispatchToProps)(UserProfileMain); //second error (133:124)

已经尝试过使用UserProfileMain as any 但是遇到了其他一堆错误

也许你知道一些最佳实践

希望大家多多支持

最佳答案

你需要改变你的

actions: bindActionCreators(sessionActions, dispatch)

线到

actions: bindActionCreators<any>(sessionActions, dispatch)

或更具体的内容,因为 redux typescript 定义正在使用 generics .

See the redux typescript definitions here .

<>中的泛型应该是sessionActions的同类型,也是bindActionCreators()函数调用的返回值。

关于reactjs - 如何解决 typescript Redux 连接错误 ActionCreatorsMapObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42888184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com