2022.08.31 特赞面试

毕竟大厂的面试,不得不说是真的难,一点八股都没问,上来就是一道题,要求给出优化的思路,详细讲讲,讲不出来基本就凉了,后面也就随便问问而已。 对于大厂感觉还是自己实力远远不够,有没有大佬知道这个怎么优化的?谢谢!

/**
 * 一份 Empty 组件代码,用作空内容展示,当前实现比较粗糙,请加以重构优化,使其更符合 react 最佳实践,并提升代码可读性、可维护性等指标。
 */

import React, { CSSProperties } from 'react';

const emptyWrapperClass = 'empty-wrapper';
const imageClass = 'empty-image';
const footerClass = 'empty-footer';

const URL = '/img/';

const Error = `${URL}Error.png`;
const NoPermission = `${URL}NoPermission.png`;
const PageNotFound = `${URL}PageNotFound.png`;
const NoContent = `${URL}NoContent.png`;

export type preDefinedImageType = 'NoPermission' | 'PageNotFound' | 'NoContent' | 'Error'

export interface EmptyProps {
  image: preDefinedImageType | React.ReactNode;
  className?: string;
  style?: CSSProperties;
  children?: React.ReactNode;
  imageStyle?: CSSProperties;
}

export const Empty = (props: EmptyProps) => {

  const { image, className, children, style } = props;

  let imgSrc = '';
  const isPredefinedImage = typeof image === 'string';
  if (isPredefinedImage) {
    if (image === 'NoPermission') {imgSrc = NoPermission};
    else if (image === 'PageNotFound') {imgSrc = PageNotFound};
    else if (image === 'NoContent') {imgSrc = NoContent};
    else if (image === 'Error') {imgSrc = Error};
  }

  const ImageJSX = ({ src }: { src: string }) => {
    return (
    <img
       className={imageClass}
       style={props.imageStyle}
       src={src}
       width={112}
       height={80}
       alt=""
     />
    );
  };

  const Image = isPredefinedImage ? <ImageJSX src={ imgSrc }/> : image;

  const wrapperClass = [className, emptyWrapperClass].filter(Boolean).join(' ');

  return (
   <div
     className={wrapperClass}
     style={style}
   >
     {Image}
     <div className={footerClass}>{children}</div>
   </div>
);
};

这是一个react的函数式组件,现有代码可以顺利运行得出正确结果,但架构上存在问题,请说明有哪些地方存在问题,并且可以进行哪些优化?

我个人当时不太清楚这块没想出来,猜想可以用上hooks?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值