我有一个React Component
,它使用connect
,withRouter
并接收自定义属性。我正在尝试将其转换为TypeScript,我想知道自己是否正确执行了此操作。至少我现在没有错误。
这是显示概念的代码:
import * as React from 'react'
import { connect } from 'react-redux';
import { withRouter, RouteComponentProps } from 'react-router';
import {
fetchTestLists,
newTestList,
displayTestList,
} from '../../../actions/index';
interface StateProps {
testList: any; // todo: use the type of state.myList to have validation on it
}
interface DispatchProps {
fetchTestLists: () => void;
newTestList: () => void;
displayTestList: (any) => void; // todo: replace any with the actual type
}
interface Props { // custom properties passed to component
className: string;
}
type PropsType = StateProps & DispatchProps & Props;
class MyTest extends React.Component<PropsType & RouteComponentProps<{}>, {}> {
constructor(props) {
super(props);
this.handleCellClick = this.handleCellClick.bind(this);
this.newTestList = this.newTestList.bind(this);
}
componentDidMount() {
this.props.fetchTestLists();
}
handleCellClick(row, column, event) {
this.props.displayTestList(row);
}
newTestList(e) {
this.props.newTestList()
}
render() {
return (
<div className={this.props.className}>
</div>
);
}
}
const mapStateToProps = (state): StateProps => ({
testList: state.myList, // todo: define a type for the root state to have validation here
});
const dispatchToProps = {
fetchTestLists,
newTestList,
displayTestList,
};
export default withRouter<Props & RouteComponentProps<{}>>(connect<StateProps, DispatchProps>(
mapStateToProps,
dispatchToProps,
)(MyTest) as any);
该组件的用法如下:<MyTest className={"active"} />
我必须进行大量实验才能使此工作正常。例如:
1)当我忽略withRouter的类型时,如下所示:export default withRouter(connect...
然后我得到TS2339: Property 'className' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<Pick<RouteComponentProps<any>, never>, C...'.
有人在这里提出了这个建议:React router in TypeScript- both router and own props,尽管我对这个概念不了解。
2)如果您想知道最后一行as any
,这与https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18999有关,并且我收到没有它的错误:
TS2345: Argument of type 'ComponentClass<Pick<any, never>> & { WrappedComponent: ComponentType<any>; }' is not assignable to parameter of type 'ComponentType<Props & RouteComponentProps<{}>>'.
Type 'ComponentClass<Pick<any, never>> & { WrappedComponent: ComponentType<any>; }' is not assignable to type 'StatelessComponent<Props & RouteComponentProps<{}>>'.
Type 'ComponentClass<Pick<any, never>> & { WrappedComponent: ComponentType<any>; }' provides no match for the signature '(props: Props & RouteComponentProps<{}> & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.
那么这是正确的方法吗?您在哪里看到问题?我基本上使用的是所有最新版本,这是我的package.json中的一个片段:
"react": "^16.2.0",
"redux": "^3.7.2",
"react-dom": "^16.2.0",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-router-redux": "^4.0.8",
...
"typescript": "^2.7.2",
"@types/react-redux": "^5.0.15",
"@types/react-router": "^4.0.22",
"@types/react-router-dom": "^4.2.4",
"@types/react": "^16.0.38",
"@types/react-dom": "^16.0.4",
我尝试重写您的示例,并得到以下代码:
import * as React from 'react';
import { connect } from 'react-redux';
import { RouteComponentProps, withRouter } from 'react-router';
import {
fetchTestLists,
newTestList,
displayTestList,
} from '../../../actions/index';
import { Dispatch, bindActionCreators, AnyAction } from 'redux';
interface IStateProps {
testList: IListType; // todo: use the type of state.myList to have validation on it
}
interface IDispatchProps {
fetchTestLists: () => AnyAction;
newTestList: () => AnyAction;
displayTestList: (value: string) => AnyAction; // todo: replace any with the actual type
}
interface IProps { // custom properties passed to component
className: string;
}
type PropsType = IStateProps & IDispatchProps & IProps;
class MyTestComponent extends React.Component<PropsType & RouteComponentProps<{}>, {}> {
constructor(props: PropsType & RouteComponentProps<{}>) {
super(props);
this.handleCellClick = this.handleCellClick.bind(this);
this.newTestList = this.newTestList.bind(this);
}
public componentDidMount() {
this.props.fetchTestLists();
}
public handleCellClick(row, column, event) {
this.props.displayTestList(row);
}
public newTestList(e) {
this.props.newTestList();
}
public render(): JSX.Element {
return (
<div className={this.props.className}>
</div>
);
}
}
export const MyTest = connect(
(state: IAppState, ownProps: IProps) => ({
testList: state.testList,
...ownProps,
}),
(dispatch: Dispatch) => bindActionCreators<AnyAction, Pick<IDispatchProps, keyof IDispatchProps>>(
{ displayTestList, fetchTestLists, newTestList },
dispatch,
),
)(withRouter(MyTestComponent));
interface IListType {
someProp: string;
}
interface IAppState {
testList: IListType;
differentList: IListType;
}
我已将导出默认值更改为将带有connect和withRouter HOC的包装好的MyTestComponent类的结果分配给MyTest。然后我像这样导入MyTest组件
import { MyTest } from './MyTest'
我添加了接口来描述从Parent组件传递来的所有属性,还以不同的方式使用withRouter和connect(对我来说更易理解)。
希望对您有所帮助
我通过强制安装@ types / react-redux软件包来解决此问题。我刚刚从4.4.5更新到5.0.15。
运行新的npm install --save @types/[email protected]
可能很值得。