反应PropTypes与流量

反应PropTypes与流量

本文介绍了反应PropTypes与流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PropTypes和Flow涵盖了类似的内容,但是使用的是不同的方法. PropTypes可以在运行时向您发出警告,这有助于快速找到来自服务器等的格式错误的响应.但是,Flow似乎是未来,并且泛型等概念是一种非常灵活的解决方案.另外,Nuclide提供的自动补全功能对Flow来说是一个很大的优势.

PropTypes and Flow cover similar things but are using different approaches. PropTypes can give you warnings during runtime, which can be helpful to quickly find malformed responses coming from a server, etc. However, Flow seems to be the future and with concepts like generics is a very flexible solution. Also the autocompletion offered by Nuclide is a big plus for Flow.

我现在的问题是,在开始新项目时哪种方法最好.还是同时使用Flow和PropTypes是一个好的解决方案?两者都使用的问题是您编写了很多重复的代码.这是我编写的音乐播放器应用程序的示例:

My question now is which is the best way to go, when starting a new project. Or could it be a good solution to use both, Flow and PropTypes? The problem with using both is that you write a lot of duplicate code. This is an example of a music player app I wrote:

export const PlaylistPropType = PropTypes.shape({
    next: ItemPropTypes,
    current: ItemPropTypes,
    history: PropTypes.arrayOf(ItemPropTypes).isRequired
});

export type Playlist = {
    next: Item,
    current: Item,
    history: Array<Item>
};

两个定义基本上都包含相同的信息,并且当数据类型更改时,两个定义都需要更新.

Both definitions basically contain the same information and when the data type is changed, both definitions need to be updated.

我发现了这个 babel插件,可以将类型声明转换为PropType,这可能是一个解决方案

I found this babel plugin to convert type declarations to PropTypes, which might be a solution.

推荐答案

问了这个问题一年后,我想更新一下我在这个问题上的经历.

One year after asking this question, I wanted to give an update about how my experiences with this problem.

随着Flow的发展,我开始用它输入代码库,并且没有添加任何新的PropType定义.到目前为止,我认为这是一个好方法,因为如上所述,它不仅使您可以键入prop,而且还可以键入代码的其他部分.例如,当您拥有状态中的道具副本时,这非常方便,可由用户修改.而且,IDE中的自动补全功能非常棒.

As Flow evolved a lot, I started typing my codebase with it and did not add any new PropType definitions. So far, I think this is good way to go, because as mentioned above, it allows you to not only type props but other parts of your code, too. This comes in really handy for example when you have a copy of you props in the state, that can be modified by the user.Also, auto-completion in IDEs is an awesome gain.

一个方向或另一个方向的自动转换器并没有真正起飞.因此,对于新项目,我现在真的建议使用Flow over PropTypes(以防您不想重复输入两次).

Automatic converters in one or the other direction didn't really take off. So, for new projects, I would now really recommend using Flow over PropTypes (in case you don't want to do the typing twice).

这篇关于反应PropTypes与流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:10