简体   繁体   English

React 构造函数声明 props 崩溃智能感知 vscode

[英]React constructor declare props crash intellisense vscode

Any raison why i cant declare my default props like this?为什么我不能像这样声明我的默认道具有什么理由吗? Am new in modern front-end and am come from js vanilla, sorry if seem hard to me understand the logic !我是现代前端的新手,来自 js vanilla,如果我觉得很难理解逻辑,对不起!

class MenueButtons extends Component {
    constructor(props) {
        super(props);
        this.props = {
            /**@type {number} description*/
            count: props?.count || 0,
            /**@type {string} description*/
            test2: props?.test2 || '',
        }
    }

this append if a avoid to pass all props defined ex: <MenueButtons count='0' />这个 append 如果避免传递所有定义的道具,例如: <MenueButtons count='0' /> 图片 Warning: MenueButtons(...): When calling super() in `MenueButtons`, make sure to pass up the same props that your component's constructor was passed.

the only way i can see is proceed like this after the class !我能看到的唯一方法是在 class 之后像这样继续!

MenueButtons.defaultProps = {
    count: 0,
    test2: '',
};

But for me it look weird, and i lost my intellisense in VsCode ?但对我来说它看起来很奇怪,我在 VsCode 中失去了智能 Any way to declare my props in constructor and get intellisense work fine ?有什么方法可以在构造函数中声明我的道具并让智能感知工作正常吗?

I try declare in constructor because it the only way to get intelligence work like this.我尝试在构造函数中声明,因为它是获得智能工作的唯一方法。 Declare with defaultProps , break suggest and declarative check.使用defaultProps声明,中断建议和声明性检查。


with declare in constructor在构造函数中声明图片图片


with declare only defaultProps仅声明 defaultProps 图片


EDIT: Ok after many test, this look more intuitive and logic for me.编辑:好的,经过多次测试,这对我来说看起来更直观和逻辑。 100% compatible with intellisense from vsCode IDE. 100% 兼容来自 vsCode IDE 的智能感知。


/**Class description */
class MenueButtons extends Component {
    constructor(props) {
        super(props);
        /**@type {{aaa?: number, bbb?: string }} */
        this.props
    }

    render() {

not yet perfect, if you have other cleaner idea , or more logic, i am open to any suggestion thank you.还不完美,如果您有其他更清晰的想法或更多逻辑,我愿意接受任何建议,谢谢。 I can maybe also create a typedef top of the document for share in defaultProps !我也许还可以在defaultProps中创建文档的typedef顶部以共享!

    /**
        * @typedef {Object} _defaultProps - ...descriptions
        * @property {number} [_defaultProps.aaa] - ...descriptions
        * @property {string} [_defaultProps.bbb] - ...descriptions
    */
       /**@type {_defaultProps} */
        this.props

thanks for any help谢谢你的帮助

I guess the main problem here is that you try to reassign this.props .我想这里的主要问题是您尝试重新分配this.props Props should be treated as read-only data within your component.道具应被视为组件中的只读数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM