gpt4 book ai didi

reactjs - TypeScript linting 错误 : ref does not exist on component

转载 作者:行者123 更新时间:2023-12-04 02:45:10 25 4
gpt4 key购买 nike

尝试添加 ref 时对于我的组件,我收到一个 linting 错误:

TS2339: Property 'childNavRef' does not exist on type 'Navigation'

enter image description here

如何正确附加 TypeScript React 组件的引用?谢谢,您可以在下面看到该组件的代码以及 tsconfig 和 eslint。

导航.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).
}
...

关于reactjs - TypeScript linting 错误 : ref does not exist on component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57467484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com