我定义了一个 React 组件并使用 Flow 来检查它:
/** @flow */
/** @jsx React.DOM */
var React = require('react');
var Hello = React.createClass({
propTypes: {
content: React.PropTypes.string.isRequired
},
render: function() {
return (
<section>hello</section>
);
}
});
module.exports = Hello;
但是flow报错:
/Users/twer/workspace/yyy/a.js:16:16,20: ReactElement
Missing annotation
因为流需要一个明确类型的模块:http://flowtype.org/docs/type-annotations.html#module-boundaries
对于 React 组件,我需要在 render
方法上声明类型,所以我必须像这样修复它:
render: function(): ?ReactElement {
return (
<section>hello</section>
);
}
问题是,它会破坏我的 IDEA 的格式化程序(因为 ?ReactElement
的 ?
)
?ReactElement
还有别的地方放吗?
最佳答案
我相信您现在已经想通了,但请注意 ?
是可选的。
render: function(): React.Element {
// ...
}
关于javascript - 如何在不破坏格式化程序的情况下为 flowtype 声明 react 组件的模块类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28458207/