gpt4 book ai didi

reactjs - Flowtype、React 和 defaultProps

转载 作者:行者123 更新时间:2023-12-03 13:20:33 24 4
gpt4 key购买 nike

经过几个小时的搜索和测试,我没有成功找到任何解决方案来让流程理解defaultProps

参见Try Flow example

使用这个模型是不一致的:

type PropsType = {|
size?: 'small' | 'big',
|};


class Letter extends PureComponent<PropsType> {

static defaultProps = {
size: 'small',
}

}

谢谢

编辑

根据 Tomasz Mularczyk 的建议,我创建了 another Try Flow example与:

type PropsType = {|
size: 'small' | 'big',
|};

而不是

type PropsType = {|
size?: 'small' | 'big',
|};

但是

defaultProp size 不再输入:

static defaultProps = {
size: 'BAD DEFAULT PROPS', // no error (?!)
}
<小时/>

解决方案

查看 Tomasz Mularczyk 的回答

+

不要等待当前组件上的流错误(在我的例子中),而是等待它将被导入和声明的位置。(似乎是半静态不是?)

最佳答案

只需从 size 类型中删除 ? 即可。 Flow 将自动看到您创建了 defaultProps 并且不需要它。

来自docs:

To type default props add a static defaultProps property to your class.

...

React also supports default props on stateless functional components. Similarly to class components, default props for stateless functional components will work without any extra type annotations.

关于reactjs - Flowtype、React 和 defaultProps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49598652/

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