簡體   English   中英

TypeScript掉毛錯誤:組件上不存在ref

[英]TypeScript linting error: ref does not exist on component

當嘗試向我的組件添加引用時,出現提示錯誤:

TS2339:類型“ Navigation”上不存在屬性“ childNavRef”

在此處輸入圖片說明

如何正確附加打字稿反應組件的參考? 謝謝,您可以在下面看到該組件的代碼以及tsconfig和eslint。

navigation.tsx:

import * as React from 'react';

interface MyState {
  show: boolean;
}

export default class Navigation extends React.Component<{}, MyState> {
  public constructor() {
    super({});
    this.state = {
      show: true,
    };
    this.childNavRef = React.createRef();
  }

  public render(): React.ReactElement {
    return (
      <nav>
        {
          this.state.show
          && (
          <div ref={this.childNavRef}>
            This is a component
          </div>
          )
        }
      </nav>
    );
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "es6",
    "moduleResolution": "node",
    "target": "es5",
    "jsx": "react",
    "allowJs": true,
    "baseUrl": ".",
    "paths": {
      "actions/*": ["src/app/redux/actions/*"],
    }
  }
}

.estlintrc:

module.exports = {
  env: {
    'jest/globals': true
  },
  extends: [
    'airbnb',
    'plugin:@typescript-eslint/recommended',
  ],
  globals: {
    'document': true,
    'window': true,
  },
  overrides: [
    {
      'files': ['**/*.tsx'],
      'rules': {
        'react/prop-types': 'off'
      }
    }
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    project: './tsconfig.json',
  },
  plugins: ['@typescript-eslint', 'jest'],
  rules: {
    'import/no-extraneous-dependencies': [
      'error',
      {
        'devDependencies': [
          '**/*.stories.jsx',
          '**/*.stories.tsx',
          '**/*.test.jsx',
          '**/*.test.js',
          '**/*.test.tsx',
          '**/*.test.ts',
          'setupTests.js',
        ],
      },
    ],
    '@typescript-eslint/camelcase': ['error', { 'properties': 'never' }],
    'indent': 'off',
    '@typescript-eslint/indent': [
      'error',
      2,
      {
        'ignoredNodes': ['JSXElement'],
        'SwitchCase': 1,
      },
    ],
    '@typescript-eslint/explicit-function-return-type': ['error', {
      'allowExpressions': true,
      'allowTypedFunctionExpressions': true
    }],
    'semi': 'off',
    '@typescript-eslint/semi': ['error'],
    'react/destructuring-assignment': [false, 'always', { 'ignoreClassFields': true }],
    'react/jsx-filename-extension': [1, { 'extensions': ['.jsx', '.tsx'] }],
  },
  settings: {
    'import/extensions': [
      '.js',
      '.jsx',
      '.ts',
      '.tsx',
    ],
    'import/resolver': {
      webpack: {
        config: 'webpack.common.js',
      }
    }
  },
};

您必須先聲明成員,然后才能使用它們。

嘗試這個:


export default class Navigation extends React.Component<{}, MyState> {
  // Declare the private member variable
  private childNavRef: React.RefObject<HTMLDivElement>; 

  public constructor() {
    super({});
    this.state = {
      show: true,
    };
    this.childNavRef = React.createRef();
  }
  ...

您也可以嘗試這樣做,並讓TypeScript自動推斷變量類型:


export default class Navigation extends React.Component<{}, MyState> {
  // Declare AND initialize the private member variable
  private childNavRef = React.createRef();

  public constructor() {
    super({});
    this.state = {
      show: true,
    };

    // Note that you no longer have to instantiate childNavRef here anymore, 
    // as TypeScript will do that automatically (it will actually compile to something
    // like in the first example, where ref is set after the super call in the constructor).
  }
  ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM