Typescript + React + Redux + reactrouter:“属性在IntrinsicAttributes和IntrinsicClassAttributes类型上不存在”将道具从父级传递给子级

时间:2018-12-03 21:13:37

标签: reactjs typescript redux react-router

我正在从事一个涉及Typescript,React,Redux和react-router的项目。当我尝试将道具从ParentComponent传递到与Redux连接的ChildComponent时,打字稿向我抛出以下错误:

  

错误:(20,17)TS2339:类型上不存在属性'exampleProp'   '内在属性和   IntrinsicClassAttributes,从不>,任何,任何>>和只读<{儿童?:   ReactNode; }>和只读<...>'。

子组件看起来像这样:

interface ChildComponentProps {
    user: UserData;
}

interface PropsToPass {
    exampleProp: string;
}

class ChildComponent extends React.Component<ChildComponentProps & PropsToPass, any> {
    constructor(props: ChildComponentProps & PropsToPass) {
        super(props);
    }

    render() {
        return (
            <div>
                Content
            </div>
        );
    }
}

const mapStateToProps = (state: ApplicationState) => ({
    user: state.authentication.user
});

const mapDispatchToProps = (dispatch: Function) => ({
});

export default withRouter(connect<{}, {}, PropsToPass>(mapStateToProps, mapDispatchToProps)(ChildComponent) as any);

父组件看起来像这样:

interface ParentComponentProps  {

}

class ParentComponent extends React.Component<ParentComponentProps, any> {
    constructor(props: ParentComponentProps) {
        super(props);
    }

    render() {
        return (
            <ChildComponent
                exampleProp="Test" // This line is highlighted as being problematic
            />
        );
    }
}

const mapStateToProps = (state: ApplicationState) => ({
});

const mapDispatchToProps = (dispatch: Function) => ({
});

export default connect(mapStateToProps, mapDispatchToProps)(ParentComponent);

我的编辑器突出显示了将“ test”作为exaampleProp值传递给子组件的那一行。 为了解决我的问题,我查看了this post,但不幸的是,建议的解决方案对我不起作用。我认为这与我使用withRouter函数导出子组件有关。我不确定我在做什么错。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

即使我不太了解为什么仍要解决此问题,我还是设法解决了该问题,因此,如果有人理解,可以随时提供更多有关此问题起因的信息。

我所做的就是更改子组件的最后一行:

export default withRouter(connect<{}, {}, PropsToPass>(
  mapStateToProps,
  mapDispatchToProps
)(ChildComponent) as any);

export default withRouter<PropsToPass & RouteComponentProps>(connect<{}, {}, PropsToPass>(
  mapStateToProps,
  mapDispatchToProps
)(ChildComponent) as any);
相关问题