简体   繁体   English

反应 Javascript - Visual Studio 代码不会自动完成 object 属性

[英]React Javascript - Visual Studio Code doesn't auto-complete object properties

I have a React HOC that propagate an instance of a class to the children.我有一个 React HOC,它将 class 的实例传播给孩子们。

import React from "react";

import ObjContext from "../../context/Obj/ObjContext";

const withObj = (Component) => (props) => (
  <ObjContext.Consumer>
    {(obj) => <Component {...props} obj={obj} />}
  </ObjContext.Consumer>
);

export default withObj;

Now, if in one of the child, I start coding, my code editor (VS Code Studio) doesn't display the properties of the object.现在,如果我在其中一个孩子中开始编码,我的代码编辑器(VS Code Studio)不会显示 object 的属性。

When I do props.obj.当我做props.obj. the editor doesn't show me all the stuff which is inside the object.编辑器没有向我展示 object 中的所有内容。

Instead, if I do const obj = new Obj() directly, I can see them.相反,如果我直接执行 const obj = new Obj() ,我可以看到它们。

Why is that?这是为什么? Is impossible to see the data which is inside the object that is propagated from a HOC and received via props?不可能看到 object 内部的数据,这些数据是从 HOC 传播并通过道具接收的?

Any workaround?任何解决方法?

Thank you.谢谢你。

As said in the comments, the real answer here is TypeScript.正如评论中所说,这里的真正答案是 TypeScript。 To see how your vs code editor would react with typescript, you can quickly do:要查看您的 vs 代码编辑器对 typescript 的反应,您可以快速执行以下操作:

// Inside child component:
import Obj from 'path/to/obj'
...
export class ChildComponent extends React.Component {
    this.obj: Obj;
    constructor(props) {
        super(props);
        this.obj = this.pros.obj;
        this.obj.something() // something would be proposed by ide
    }
...
}

(Ofc the vs code will growl saying that types are not eligible in a.js file and you'd need.tsx instead, but for the purpose of taking a look at how TS would help with the auto-completion, its fine. (Ofc 的 vs 代码会咆哮说类型在 a.js 文件中不符合条件,而您需要.tsx,但为了看看 TS 如何帮助自动完成,它很好。

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

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