暴露React组件PropType [英] Exposing React Component PropTypes

查看:83
本文介绍了暴露React组件PropType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发React Native中的一些可重用组件。我为组件定义了一些 PropTypes ,但是当我将组件发布到我的私有 npm存储库时,Props Intellisense无法正常工作。

I am developing some reusable components in React Native. I have defined some PropTypes for components but when I publish the components to my private npm repository the Props Intellisense doesn't work.

作为一个示例,如果我定义了 title propType ,则如果我开始输入 t ...,则将该组件添加到另一个组件中不会向我建议 title

As an example if I define a title propType, when I add the component to another component if I start typing t... it doesn't suggest title to me.

如何解决此问题?

推荐答案

您可以使用扩充来做到这一点。只需在扩充部分中定义道具,它们就会在您键入时显示在建议中。

You can use augments to do that. Just define your props in the augments part they will appear in the suggestions as you type.

/**
* @augments {Component<{ title:string, anotherProp:boolean>}
*/
class MyReactClass extends React.Component {

}

如果您使用的是vscode,我已经创建了一个简单的扩展程序来自动执行此操作。但这需要您定义propTypes。

If you're using vscode, I've created a simple extension to do that automatically. But it requires you to define propTypes.

https://marketplace.visualstudio.com/items?itemName=cuneyt-aliustaoglu.vscode-react-proptypes-intellisense

这篇关于暴露React组件PropType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆