gpt4 book ai didi

javascript - super() : expected 1-2 arguments, 上的 typescript 错误但得到 0

转载 作者:行者123 更新时间:2023-12-05 00:28:36 25 4
gpt4 key购买 nike

我不知道我需要做什么来解决这个 TS 错误:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

declare const Ink: any;

export default class TableOfContents extends Component <{ id: string }, { rendered: boolean }> {
constructor() {
super();
this.state = { rendered: false };
}
enter image description here
更新 :
放在这里我感觉不太好,应该是什么?
enter image description here

最佳答案

The constructor for a React component is called before it is mounted.When implementing the constructor for a React.Component subclass, youshould call super(props) before any other statement. Otherwise,this.props will be undefined in the constructor, which can lead tobugs.


https://reactjs.org/docs/react-component.html#constructor
所以,你需要写:
constructor(props) {
super(props);
this.state = { rendered: false };
}
你还可以为你的 props 和 state 定义接口(interface)来修复所有 TS 警告和更好的智能感知:
interface IProps {
id: string
}

interface IState {
rendered: boolean
}

export default class TableOfContents extends Component<IProps, IState> {
constructor(props: IProps) {
super(props)
this.state = {
rendered: false,
}
}
}
如果你的编辑提示并且不接受隐含的 any ,可以设置 "noImplicitAny": falsecompilerOptions在 tsconfig 文件中。

关于javascript - super() : expected 1-2 arguments, 上的 typescript 错误但得到 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312030/

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