流-缺少类型注释

时间:2019-05-14 11:00:13

标签: reactjs flowtype

很新,正在尝试修复我的代码以包括流。这是我目前的代码,我添加了流类型检查,现在出现错误,因此需要正确注释我的代码:

// @flow
import React, { Component } from 'react';
import { Manager, Reference, Popper } from 'react-popper';

import cx from 'classnames';
import css from './Tooltip.css';
import animationsCss from './TooltipAnimations.css';

type Props = {
  theme: string,
  eventsEnabled: boolean,
}
class Tooltip extends Component<Props> {
  static defaultProps = {
    theme: 'light',
    eventsEnabled: true
  };

  firstOrderPlacement(placement) {
    if (!placement) return null;
    return placement.split('-')[0];
  }

  arrowDirectionClass(firstOrderPlacement) {
    switch (firstOrderPlacement) {
      case 'right':
        return css.arrowLeft;
      case 'left':
        return css.arrowRight;
      case 'top':
        return css.arrowDown;
      case 'bottom':
        return css.arrowUp;
      default:
        return css.arrowUp;
    }
  }

  render() {
    const { placement, className, children, fadeIn, theme, eventsEnabled } = this.props;

    return (
      <Popper placement={placement} eventsEnabled={eventsEnabled}>
        {({ ref, style, placement }) => {
          const firstOrderPlacement = this.firstOrderPlacement(placement);
          const arrowDirectionClass = this.arrowDirectionClass(firstOrderPlacement);
          const subContainerStyle = {
            display: 'flex',
            flexDirection:
              firstOrderPlacement === 'top' || firstOrderPlacement === 'bottom' ? 'column' : 'row',
          };
          const childrenContainerClassName = cx(
            css.childrenContainer,
            css.wrapper,
            theme === "dark" ? css.dark : css.light
          );
          const content = <div className={childrenContainerClassName}>{children}</div>;
          const subContainerClassName = fadeIn ? cx(animationsCss.fadeIn, className) : className;

          return (
            <div
              ref={ref}
              className={cx(css.container, css.mobileTooltip)}
              style={style}
              data-placement={placement}
            >
              <div className={subContainerClassName} style={subContainerStyle}>
                {(firstOrderPlacement === 'left' || firstOrderPlacement === 'top') && content}
                <div>
                  <div className={cx(css.arrow, arrowDirectionClass)} />
                </div>
                {(firstOrderPlacement === 'right' || firstOrderPlacement === 'bottom') && content}
              </div>
            </div>
          );
        }}
      </Popper>
    );
  }
}

export { Manager as TooltipManager, Reference as TooltipReference, Tooltip };

我相信我需要将这些添加到我的道具中。这些正确吗?

  placement: string,
  className?: string,
  children?: any,
  fadeIn: any,

我缺少这两个的类型注释,我不确定该如何进行:

  firstOrderPlacement(placement) {..}

  arrowDirectionClass(firstOrderPlacement) {..}

有帮助吗?

2 个答案:

答案 0 :(得分:2)

像这样注释Props

type Props = {
  ...
  placement: string,
  className?: string,
  children?: any,
  fadeIn: any,
  ...
}

Placement参数为 string firstOrderPlacement(placement) {..},函数的返回值为nullstring,因此可以使用maybe type用于注释:

firstOrderPlacement(placement: string): ?string => {...}

或者使用联合类型,因为maybe type涵盖了undefined

type FirstOrder = string | null;

firstOrderPlacement函数的结果是arrowDirectionClass的参数。所以参数类型:

arrowDirectionClass(firstOrderPlacement: ?string): string => {...}

或者:

arrowDirectionClass(firstOrderPlacement: FirstOrder): string => {...}

答案 1 :(得分:0)

  

这些对吗?

尝试一下,找出答案!

请记住,使用any是一种快捷方式,基本上只是关闭了键入功能。对于需要反应元素的children,通常应使用React.node。如果您基本上想要任意嵌套的类似DOM的内容(例如react元素,元素数组,文本字符串等),这将起作用。fadeIn看起来像是可能字符串,但这有点很难知道它的来源。

  

我缺少这两个的类型注释,我不确定该如何进行:

Flow希望您键入函数的参数。像这样:

firstOrderPlacement(placement: string) {

或其他。