this.collapsible
是类型为 ?HTMLElement
的 React Ref。我正在尝试像这样设置 style
:
this.collapsible.style.height = ...
而且,尽管将上面的内容包装在 if (this.collapsible && this.collapsible.style)
中,似乎 flow 仍然相信 this.collapsible.style
可以为空。
Cannot get this.collapsible.style because property style is missing in null or undefined [1].
97│
98│ componentDidMount() {
99│ if (this.collapsible && this.collapsible.style) {
100│ this.collapsible.style.height = getInitialHeight(this.props);{
:
[1] 148│ collapsible: ?HTMLElement;
如何解决?
最佳答案
正如@John Ruddell 指出的那样,给变量起别名是可行的。
if (this.collapsible) {
const collapsible: HTMLElement = this.collapsible;
collapsible.style.height = getInitialHeight(this.props);
我无法找到无需创建新变量即可转换类型的好方法。
关于javascript - 即使在 if 语句中包装后,Flow 仍认为该值为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434877/