简体   繁体   English

在React中动态加载的CSS无法间歇工作

[英]Dynamically loaded CSS in React not working intermittently

I've made a "pluggable" system in React, which dynamically runs tiny "apps" which consist of an HTML, JS and CSS file. 我在React中制作了一个“可插拔”系统,该系统可动态运行由HTML,JS和CSS文件组成的微型“应用程序”。 The HTML and CSS files are optional. HTML和CSS文件是可选的。 They intercommunicate through the window object. 它们通过窗口对象进行相互通信。

I'm dynamically loading the three files here, but I'm having the problem that my CSS classes fail to work 1/5 of the time. 我在这里动态加载这三个文件,但我遇到的问题是我的CSS类无法正常工作1/5。 They don't even seem to get parsed since I cannot manually apply them in Chrome devtools either. 它们似乎都没有被解析,因为我也无法在Chrome devtools中手动应用它们。

I've tried using both link and style tags to load the CSS, but both have the same problem. 我试过同时使用linkstyle标签来加载CSS,但是两者都有相同的问题。 Even a 1000ms setTimeout between the CSS and HTML injection doesn't help. CSS和HTML注入之间的甚至1000ms setTimeout也无济于事。 CSS parsing consistently fails roughly every third time the component mounts.. CSS解析始终大约在组件每次安装时每隔三次失败。

I've tried Chrome, Firefox, and Safari. 我已经尝试过Chrome,Firefox和Safari。 Same problem in all three. 这三个问题都相同。

I'm kind of stuck, I'd love to get some feedback on this.. 我有点被困住了,我很乐意收到一些反馈。

Here is a video of the issue: (the "app" here is a simple SVG file viewer) http://www.giphy.com/gifs/dvHjBBolgA1xAdyRsv 这是有关此问题的视频:(“应用程序”是一个简单的SVG文件查看器) http://www.giphy.com/gifs/dvHjBBolgA1xAdyRsv

  const windowInitialized = useElementBlockInitialization({
    id: elementBlockID,
    payload: payload,
    onResult: onResult
  });
  const [styleAndHTMLInitialized, setStyleAndHTMLInitialized] = useState(false);

  // after some properties are set in Window, run this effect
  useEffect(() => {
    let gettingStyleAndHTML = false;
    if (windowInitialized) {
      gettingStyleAndHTML = true;
      getStyleAndHTML().then(({ styleBody, htmlBody }) => { // async function that fetches some html and css as a string (both potentially null)
        if (gettingStyleAndHTML) {
          if (styleBody) {
            const styleElement = document.createElement('style');
            styleElement.type = 'text/css';
            styleElement.appendChild(document.createTextNode(styleBody));
            document.head.appendChild(styleElement);
          }
          if (htmlBody) {
            // containerElement is a ref
            containerElement.current.innerHTML = htmlBody;
          }
          setStyleAndHTMLInitialized(true);
        }
      });
    }
    return () => {
      gettingStyleAndHTML = false;
    };
  }, [windowInitialized]);

  // after the CSS and HTML is injected, run this hook
  useEffect(() => {
    if (styleAndHTMLInitialized) {
      const scriptElement = document.createElement('script');
      scriptElement.setAttribute('data-eb-container-id', containerElementID);
      scriptElement.setAttribute('data-eb-id', elementBlockID);
      scriptElement.setAttribute('src', makeElementBlockBaseURL() + '.js');
      document.head!.appendChild(scriptElement);

      return () => {
        scriptElement.remove();
      };
    }
    return;
  }, [styleAndHTMLInitialized]);

  // only render the container once the window properties are set
  return windowInitialized ? (
    <Container ref={containerElement} id={containerElementID} />
  ) : null;

I figured it out. 我想到了。

My automatically generated class names occasionally started with a number. 我自动生成的班级名称有时以数字开头。 CSS class names can not apparently start with a number! CSS类名显然不能以数字开头!

Do'h.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM