当前位置: 首页 > 知识库问答 >
问题:

类型中缺少不必手动传递给子组件的属性

韩志专
2023-03-14

在我的子组件中,我定义了道具接口,并将其包含在React中。组件。

然后需要将这些道具从父组件传递到子组件。到目前为止,一切都是合理的。。

但是,当我使用react router Typescript中的RouteComponents Props扩展Props接口时,也要求我传递“历史、位置、匹配”,我认为我不应该手动传递。。。

我不认为它与RouteComponentProps特别相关,因为在某些情况下,我遇到了与MapDispatchToProps和PropsFromDispatch接口相同的错误——这里对这个案例有更详细的解释

这是我的密码:

/Child.tsx

import * as React from 'react'
import { RouteComponentProps } from 'react-router';

interface Props extends RouteComponentProps { }

class Child extends React.Component<Props> {
    render() {
        return (
            <div>

            </div>
        )
    }
 }

export default Child;

/Parent.tsx

import * as React from 'react'
import Child from './Child';

export default class Parent extends React.Component {
    render() {
        return (
            <div>
                <Child />
             </div>
        )
    }
}

/Parent.tsx中出错:

<Child/>

Type '{}' is missing the following properties from type 
'Readonly<Props>': history, location, match - ts(2739)

键入脚本和反应版本:

“typescript”:“^3.2.1”,“react”:“^16.6.3”,“@types/react”:“^16.7.13”

感谢任何建议!

共有2个答案

宰父才
2023-03-14

我也有同样的问题,来自@goediaz的想法对我来说很好,唯一的区别是我在使用功能组件

const MyFunciont: React.FC<IGlobalState> = (_RouteComponentProps, props) = {
//here your code

return (
    //html code
    )
}
濮阳烨然
2023-03-14

因此,问题是由Props引起的,这些Props在RouteComponentProps类中标记为必需。

当您需要将类导出为any时,它将不带任何类型地导出您的类。

import * as React from 'react'
import { RouteComponentProps, withRouter } from 'react-router';

interface Props extends RouteComponentProps { }

class ChildImpl extends React.Component<Props> {
    render() {
        return (
            <div>

            </div>
        )
    }
 }
const Child = withRouter(ChildImpl as any);//Note: It is a workaround not an actual solution
export default Child;

然后在你的父母身上:

import * as React from 'react'
import Child from './Child';
export default class Parent extends React.Component {
    render() {
        return (
            <div>
                <Child />
             </div>
        )
    }
}

不需要道具通过。

 类似资料:
  • 反应组分 如何在组件中声明输入组件?

  • 快速问题 - 假设我有一个这样的组件: 如果我们不传递 nameA,有没有办法在 Typescript 中强制使用 nameB? 写起来

  • 我试图从后端服务器检索数据,但我得到以下错误:类型'订阅'缺少类型'HomeData'中的以下属性:aSes、聚合器更改、公告、beacon公告和11个以上。 我正在使用以下服务 以及以下界面: 我在组件中调用getData(),如下所示: 我把所有这些都放在我的html模板中

  • 这是我的。工作文件 作业失败,出现以下错误: 我用的是Azkaban 2.5和cdh5

  • 我试图将类名传递给react组件以更改其样式,但似乎无法正常工作: 我试图通过传递具有各自风格的类的名称来改变药丸的风格。我是新来的,所以也许我做得不对。谢啦

  • 英文原文:http://emberjs.com/guides/components/passing-properties-to-a-component/ 默认情况下,组件不能访问模板作用域下的属性。 例如,假设有一个用于显示一篇博客的blog-post组件: 1 2 3 4 <script type="text/x-handlebars" id="components/blog-post">