【问题标题】:Why my React [this.props] contains no element necessary for parameter capturing为什么我的 React [this.props] 不包含参数捕获所需的元素
【发布时间】:2018-04-04 03:33:22
【问题描述】:

我有一个反应类,我正在尝试从调用者那里检索参数。参考一些在线教程以及 Stackoverflow 中的答案,我应该使用以下任一方法:

  • this.props.match.params
  • queryString.parse(this.props.location.search);

但是,当我尝试使用我的 node.js 编译它时,这两种方法也会引发如下错误:

[at-loader] 中的错误 ./js/module/sample/sample01.tsx:8:29 TS2339: 属性 'firstName' 不存在于类型 'Readonly & 只读'。

[at-loader] 中的错误 ./js/module/sample/sample01.tsx:9:44 TS2339:“只读”类型上不存在属性“位置” & 只读'。

this.props

下似乎没有其他元素

这是我的源代码:

import * as React from 'react';
import { Link } from 'react-router-dom';
import * as queryString from 'query-string';

export class Textboxes extends React.Component {

  render() {
    var firstName = this.props.match.params.firstName;
    var parsed = queryString.parse(this.props.location.search);
    var firstName = parsed.firstName;
    var lastName = parsed.lastName;
    return(
    ...

谢谢。

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    如果您试图将参数传递给文本框...

    <Textboxes firstName="John" lastName="Wong/>
    

    你应该做的就是……

    export class Textboxes extends React.Component {
    
      render() {
        var firstName = this.props.firstName;
        var lastName = this.props.lastName;
        return(
        ...
    

    如果有误,请告诉我,我会删除这篇文章。

    【讨论】:

    • 嗨,JD,在编译期间使用 this.props.firstName 也会产生类似的错误。
      [at-loader] ./js/module/sample/sample01.tsx:8:29 TS2339 中的错误:“只读”类型上不存在属性“firstName” & 只读'。
    • 只是好奇,我看到你有一个 *.tsx 扩展名。你在使用 jsx 的 typescript 吗?
    • 我不是打字稿专家,但这可能是你的问题...stackoverflow.com/questions/44489338/…
    • 嗨 JD,是的,它的 jsx 打字稿。对于您共享的链接,它似乎解决了子组件的问题,但我的不是子组件:)
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多