【问题标题】:React.JS prop is not found找不到 React.JS 道具
【发布时间】:2021-03-31 08:19:56
【问题描述】:

这是我的 index.ts 代码(我在其中调用组件来显示)

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

这是 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
        ....

为什么我无法在 helloWorld.ts 文件中获取 this.props.elements ?我该怎么做?

我有错误:类型'Readonly & Readonly'。 TS2339

【问题讨论】:

标签: javascript reactjs typescript


【解决方案1】:

使用构造函数,同时在渲染方法中丢失 'this'。

   constructor(props)
{
    super(props);
}

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

只需使用props.elements 而不是this.props.elements

【讨论】:

    猜你喜欢
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2019-04-23
    • 2021-11-25
    • 2023-03-25
    相关资源
    最近更新 更多