我只是想把我的React应用升级到
react 路由-4.0.19至4.0.20
react -16.0.30至16.0.34
typescript-版本"2.7.0-insiders.20180108"
在我的应用程序中,无论我在哪里使用"withRouter",我现在都会遇到晦涩的打字错误.我甚至用"any"替换了所有界面props ,只是想让它工作起来.
import * as React from 'react';
import { Switch, Route, withRouter} from 'react-router-dom';
import { Login } from './Login';
import { connect } from 'react-redux';
import { RootAction, RootState } from './_redux';
class MainForm extends React.Component<any> {
constructor(props: any) {
super(props);
}
render() {
return (
<Switch>
<Route exact={true} path="/" component={Login}/>
<Route path="/accounts" component={AccountsView}/>
</Switch>
);
}
}
const mapStateToProps = (state: RootState) => ({
state
});
export const Main = withRouter(connect(mapStateToProps)(MainForm);
error TS2345: Argument of type 'ComponentClass> & { WrappedComponent: ComponentType; }' is not assignable to parameter of type 'ComponentType>'. Type 'ComponentClass> & { WrappedComponent: ComponentType; }' is not assignable to type 'StatelessComponent>'. Type 'ComponentClass> & { WrappedComponent: ComponentType; }' provides no match for the signature '(props: RouteComponentProps & { children?: ReactNode; }, context?: any): ReactElement | null'.
如果我将最后一行转换为:
export const Main = connect(mapStateToProps)(MainForm);
我没有错误.我在这里非常沮丧.
EDIT,我改成
export const Main = connect(mapStateToProps)(withRouter(MainForm));
就像Mayank Shukla建议的那样.但现在,我们来看看错误:
error TS2345: Argument of type 'ComponentClass>' is not assignable to parameter of type 'ComponentType<{ state: RootState; } & DispatchProp>'. Type 'ComponentClass>' is not assignable to type 'StatelessComponent<{ state: RootState; } & DispatchProp>'. Type 'ComponentClass>' provides no match for the signature '(props: { state: RootState; } & DispatchProp & { children?: ReactNode; }, context?: any): ReactElement | null'.