react 中 动态添加 class,防止图片 重复加载, 主要是 backgroundimage的二次加载会有新请求,和图片的闪烁

react 中 动态添加 class,防止图片 重复加载, 主要是 background-image的二次加载会有新请求,和图片的闪烁

let imageTopBg
  if (imgSrcBg) {
    const imgSrcBgHeight1 = imgSrcBgHeight ? imgSrcBgHeight : '98px'
    const cn = defineCss(`${imgSrcBg.replace('.', '')}ClassName`, `
        background-image: url("./uploads/images/${imgSrcBg}");
        height: ${imgSrcBgHeight1};
        border-radius: 3px 3px 0 0;
    `)
    imageTopBg = (
      <div className={cn}></div>
    )
  }

const defineCss = (className, styleBlock) => {
  const exist = document.getElementById(className)
  console.info('exist', exist)
  if (!exist) {
    const styleEl = document.createElement('style')
    styleEl.id = className
    styleEl.textContent = `
      .${className} {
        ${styleBlock}
      }
    `
    document.head.appendChild(styleEl)
  }
  return className
}
---------------------------------------------
生活的意义并不是与他人争高下,而在于享受努力实现目标的过程,结果是对自己行动的嘉奖。
↑面的话,越看越不痛快,应该这么说:

生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
原文地址:https://www.cnblogs.com/pengchenggang/p/15743925.html