reactjs – Redux connect的mapStateToProps的正确类型声明(Type
发布时间:2020-12-15 09:36:20 所属栏目:百科 来源:网络整理
导读:我有一个Redux商店,想要连接它.这是我的容器组件的摘录: interface IProps { states: IAppState; actions: IAppProps;}// mapping state to the propsconst mapStateToProps = ( state: IAppState,ownProps = {} ) = ({ states: state});// mapping actions
我有一个Redux商店,想要连接它.这是我的容器组件的摘录:
interface IProps { states: IAppState; actions: IAppProps; } // mapping state to the props const mapStateToProps = ( state: IAppState,ownProps = {} ) => ({ states: state }); // mapping actions to the props const mapDispatchToProps = ( dispatch: Redux.Dispatch<IAppState> ) => ({ actions: Redux.bindActionCreators( actions,dispatch ) }); // connect store to App @connect<IAppState,IAppProps,any>( mapStateToProps,mapDispatchToProps ) export default class App extends React.Component<IProps,{}> { //... } 编译时我收到以下问题: error TS2345: Argument of type '(state: IAppState,ownProps?: {}) => { states: IAppState; }' is not assignable to parameter of type 'MapStateToPropsParam<IAppState,any>'. Type '(state: IAppState,ownProps?: {}) => { states: IAppState; }' is not assignable to type 'MapStateToPropsFactory<IAppState,any>'. Type '{ states: IAppState; }' is not assignable to type 'MapStateToProps<IAppState,any>'. Type '{ states: IAppState; }' provides no match for the signature '(state: any,ownProps?: any): IAppState'. 我正在使用@ types / react-redux @ 4.4.44公开MapStateToProps接口.我认为我的mapStateToProps面对这个界面……但是有些事情是错的.有什么想法是什么? 解决方法
好吧,似乎@ types / react-redux接受mapStateToProps返回完全相同的类型.我希望它在映射期间修改它,如{states:AppStateTree}.相反,我修改了状态树combineReducers({states:myReducer}).所以这段代码工作正常:
interface IRootState { state: IAppState; } const mapStateToProps = ( state: IRootState ) => state; const mapDispatchToProps = { toggleOpenAddFeed }; type IProps = IRootState & typeof mapDispatchToProps; class App extends React.Component<IProps,{}> { render() { return ( <div className="main-wrapper"> <Mycomponent store={this.props} /> </div> ); } } export default connect( mapStateToProps,mapDispatchToProps )( App ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ios – 在viewboard中将view.bottom约束到superview.bottom
- [HyBrid]HyBrid混编初尝:原生和第三方JsBridge的使用
- _Actual_ Twitter格式的标签?不是你的正则表达式,而不是他
- postgresql – 在pgAdmin中看不到用户定义的数据库“types”
- ruby-on-rails – 如何在Ruby on Rails中使用jTable?
- xml – 使用包含()的XPath count()
- swift – NSTimer不会调用私有func作为选择器
- iphone – UIScrollViewDelegate没有开火
- vue v-model实现自定义样式多选与单选功能
- ruby-on-rails – Rails中的REST API参数验证