Reactjs 将react-redux连接与typescript一起使用

Reactjs 将react-redux连接与typescript一起使用,reactjs,typescript,react-router,react-redux,react-router-v4,Reactjs,Typescript,React Router,React Redux,React Router V4,我正在尝试将React-redux连接与typescript一起使用。在使用connect和withRouter时,我感到困惑 我试着像这样使用它 withRouter<any>(connect<{},ComponentProps,{}>( matchStateToProps, matchDispatchToProps )(Component)); 它工作得很好。 ComponentProps包含组件的所有属性。(包括stateProps,dispat

我正在尝试将React-redux连接与typescript一起使用。在使用
connect
withRouter
时,我感到困惑

我试着像这样使用它

withRouter<any>(connect<{},ComponentProps,{}>(
    matchStateToProps, 
    matchDispatchToProps
)(Component));
它工作得很好。
ComponentProps
包含组件的所有属性。(包括
stateProps
dispatchProps
RouteProps
,&own-props)

如何在typescript中使用react的
与路由器连接
?我应该通过什么作为路由器
连接
的道具?

我这样做:

import { compose } from 'redux';
然后:

导出默认值(
用路由器,
连接(
MapStateTops,
{
获取组件数据
},
),
)(构成部分);
我是这样做的:

import { compose } from 'redux';
然后:

导出默认值(
用路由器,
连接(
MapStateTops,
{
获取组件数据
},
),
)(构成部分);

经过一番努力,我终于可以让它工作了

// assuming all these interfaces are well defined somewhere.
type MyComponentProps = OwnProps & ComposeProps & StateProps & DispatchProps;

// the secret sauce, need to define a class constructor to pass to compose
interface MyComponentClass<MyComponentProps> extends React.ComponentClass {
  new (props: MyComponentProps): React.Component<MyComponentProps>;
}

export class MyComponent extends React.Component<MyComponentProps> {
  ...
}

export default compose<MyComponentClass<MyComponentProps>>(
  withSomeHOC(),
  connect<StateProps, DispatchProps, OwnProps>(mapStateToProps, mapDispatchToProps)
)(MyComponent);
//假设所有这些接口都在某个地方定义得很好。
键入MyComponentProps=OwnProps&ComponentProps&StateProps&DispatchProps;
//秘方,需要定义一个类构造函数来传递组合
接口MyComponentClass扩展了React.ComponentClass{
新(道具:MyComponentProps):React.Component;
}
导出类MyComponent扩展了React.Component{
...
}
导出默认组合(
使用somehoc(),
连接(MapStateTrops、mapDispatchToProps)
)(MyComponent);

以上将允许您导入组合组件并维护类型安全

经过一番努力,我终于可以让它工作了

// assuming all these interfaces are well defined somewhere.
type MyComponentProps = OwnProps & ComposeProps & StateProps & DispatchProps;

// the secret sauce, need to define a class constructor to pass to compose
interface MyComponentClass<MyComponentProps> extends React.ComponentClass {
  new (props: MyComponentProps): React.Component<MyComponentProps>;
}

export class MyComponent extends React.Component<MyComponentProps> {
  ...
}

export default compose<MyComponentClass<MyComponentProps>>(
  withSomeHOC(),
  connect<StateProps, DispatchProps, OwnProps>(mapStateToProps, mapDispatchToProps)
)(MyComponent);
//假设所有这些接口都在某个地方定义得很好。
键入MyComponentProps=OwnProps&ComponentProps&StateProps&DispatchProps;
//秘方,需要定义一个类构造函数来传递组合
接口MyComponentClass扩展了React.ComponentClass{
新(道具:MyComponentProps):React.Component;
}
导出类MyComponent扩展了React.Component{
...
}
导出默认组合(
使用somehoc(),
连接(MapStateTrops、mapDispatchToProps)
)(MyComponent);

以上将允许您导入组合组件并维护类型安全

“不行”为什么不行?发生了什么事?@我一点都不知道。老实说,我不知道上面的方法是否正确。我认为你的方法是正确的。可能还有另一个与某些属性“productList”有关的错误,但我看不到该属性的任何代码。“不工作”为什么不?发生了什么事?@我一点都不知道。老实说,我不知道上面的方法是否正确。我认为你的方法是正确的。可能还有另一个与某些属性“productList”有关的错误,但我看不到该属性的任何代码。