简体   繁体   English

找不到 React.JS 道具

[英]React.JS prop is not found

Here is the piece of my index.ts code (where I call the component to display)这是我的 index.ts 代码(我在其中调用组件来显示)

        <GlobalContext.Provider value={this.globalContext as IGlobalContext}>
            <Container maxWidth="lg">
                <Switch>
                    <Route path="" component={HelloWorld} elements={this.getResults()}/>
                </Switch>
            </Container>
        </GlobalContext.Provider>

Here is the piece of the helloWorld.ts file (the beggining)这是 helloWorld.ts 文件的一部分(开始)

import React from 'react';

export default class HelloWorld extends React.Component {

    public render(): JSX.Element {
        let elements = this.props.elements; // I Want to get the elements props passed throuth my router component
        ....

Why can't I get the this.props.elements in the helloWorld.ts file?为什么我在 helloWorld.ts 文件中无法获取 this.props.elements? How can I do it?我该怎么做?

I Have the error: Property 'elements' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode;我有错误:类型'Readonly<{}> & Readonly<{ children?: ReactNode; 上不存在属性'elements' }>'. }>'。 TS2339 TS2339

Use constructor and also lose the ' this ' in render method.使用构造函数并且在渲染方法中也丢失了“ this ”。

   constructor(props)
{
    super(props);
}

public render(): JSX.Element {
 let elements = props.elements;
            ....

Just use props.elements instead of this.props.elements只需使用props.elements而不是this.props.elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM