【问题标题】:Use Memo combine into single condition使用备忘录合并成单个条件
【发布时间】:2022-11-03 01:12:05
【问题描述】:

我添加了检查页眉和页脚是否为值的条件

        const hasHeader = !!(children as React.ReactNode[]).find((el) =>
          isComponentType(el, Modal.Header)
        );
        const hasFooter = !!(children as React.ReactNode[]).find((el) =>
          isComponentType(el, Modal.Footer)
        );
        return {
          hasHeader,
          hasFooter,
        };
      }, [children]);```

Both header and footer are having the same code, how can I make in a single condition

【问题讨论】:

    标签: reactjs


    【解决方案1】:
    const hasChildComponent = (Component) => {
      return !!(children as React.ReactNode[]).find((el) => isComponentType(el, Component));
    }
    
    return {
      hasHeader: hasChildComponent(Modal.Header),
      hasFooter: hasChildComponent(Modal.Footer),
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 2018-11-28
      • 2020-03-23
      相关资源
      最近更新 更多