Sha*_*awn 13 javascript typescript reactjs redux react-redux
我试图输入反应组件的参数时出错.我想严格键入组件的道具和状态的属性,但是当我使用Redux时,我将mapStateToProps传递给connect函数时出错.
这是组件代码:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import FileExplorer from '../components/file-explorer/file-explorer';
import { ISideMenu, ISideMenuState } from '../models/interfaces/side-menu';
class SideMenu extends Component<ISideMenu, ISideMenuState> {
render() {
return (
<div>
{this.props.fileExplorerInfo !== null &&
<FileExplorer fileExplorerDirectory={this.props.fileExplorerInfo.fileExplorerDirectory}/>
}
</div>
);
}
}
const mapStateToProps = (state: ISideMenuState) => {
return {
fileExplorerInfo: state.fileExplorer
};
};
export default connect<ISideMenu, null, ISideMenuState>(mapStateToProps)(SideMenu);
Run Code Online (Sandbox Code Playgroud)
所以错误发生在这一行:
export default connect<ISideMenu, null, ISideMenuState>(mapStateToProps)(SideMenu);
Run Code Online (Sandbox Code Playgroud)
当我将鼠标悬停在该行的"mapStateToProps"字样时,我看到了错误:
Argument of type '(state: ISideMenuState) => { fileExplorerInfo: FileDirectoryTree | null; }'
is not assignable to parameter of type 'MapStateToPropsParam<ISideMenu, ISideMenuState, {}>'.
Type '(state: ISideMenuState) => { fileExplorerInfo: FileDirectoryTree | null; }' is not
assignable to type 'MapStateToProps<ISideMenu, ISideMenuState, {}>'.
Types of parameters 'state' and 'state' are incompatible.
Type '{}' is not
assignable to type 'ISideMenuState'.
Property 'fileExplorer' is missing in type '{}'.
Run Code Online (Sandbox Code Playgroud)
这些是我在react组件中使用的两个接口:
export interface ISideMenu {
fileExplorerInfo: FileExplorerReducerState | null;
}
export interface ISideMenuState {
fileExplorer: FileDirectoryTree | null;
}
Run Code Online (Sandbox Code Playgroud)
任何洞察这个错误将不胜感激!
klu*_*gjo 24
使用泛型时,您的接口位置错误:
声明React组件时:
class Comp extends Component<ICompProps, ICompState>
Run Code Online (Sandbox Code Playgroud)
有ICompProps
和ICompState
你的组件的道具和内部状态分别.
使用connect时:
connect<IMapStateToProps, IMapDispatchToProps, ICompProps, IReduxState>
Run Code Online (Sandbox Code Playgroud)
IMapStateToProps
表示mapStateToProps()
函数返回的内容.
IMapDispatchToProps
表示mapDispatchToProps()
函数返回的内容.
ICompProps
代表你的React组件道具(与上面相同)
IReduxState
代表你的应用程序的Redux状态
所以在你的特定例子中:
声明React组件时:
class SideMenu extends Component<ISideMenu, {}>
Run Code Online (Sandbox Code Playgroud)
使用ISideMenu
的道具和{}
(空状态)的状态,你不使用任何状态.
使用连接时:
connect<ISideMenu, {}, ISideMenu, ISideMenuState>(mapStateToProps)(SideMenu);
Run Code Online (Sandbox Code Playgroud)
您可以ISideMenu
将React组件道具和返回的对象用作mapStateToProps
.但在实践中,最好创建2个独立的接口.
在我的应用程序中,我通常不会打扰mapStateToProps
返回对象,所以我只是使用:
connect<{}, {}, ISideMenu, ISideMenuState>(mapStateToProps)(SideMenu);
Run Code Online (Sandbox Code Playgroud)
希望你不介意我从上面的代码中删除一些反模式。请检查我添加的评论。我还添加了 withRouter 来更好地说明模式
import * as React from "react";
import { bindActionCreators } from "redux";
import { withRouter, RouteComponentProps } from "react-router";
import { connect } from "react-redux";
import { compose } from "recompose";
// OUR ROOT REDUX STORE STATE TYPE
import { State } from "../redux";
import FileExplorer from "../components/file-explorer/file-explorer";
// interfaces starting with 'I' is an antipattern and really
// rarely needed to be in a separate file
// OwnProps - that's what external code knows about out container
type OwnProps = {};
// this comes from redux
type StateProps = {
fileExplorer: FileDirectoryTree | null;
};
// resulting props - that what container expects to have
type Props = OwnProps & StateProps & RouteComponentProps<any>;
// no need to have a class, SFC will do the same job
const SideMenu: React.SFC<Props> = props => {
return (
<div>
{this.props.fileExplorerInfo !== null && (
<FileExplorer
fileExplorerDirectory={
this.props.fileExplorerInfo.fileExplorerDirectory
}
/>
)}
</div>
);
};
// compose (from recompose lib) because usually we have more than 1 hoc
// say let's add withRouter just for fun
export default compose<Props, OwnProps>(
withRouter,
// it's important to read the typings:
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-redux/index.d.ts
connect<StateProps, {}, {}, State>(s => ({
fileExplorerInfo: s.fileExplorer
})),
)(SideMenu);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6053 次 |
最近记录: |