简体   繁体   English

React -- 移除外部 CDN 创建的 dom 元素

[英]React -- remove dom elements created by an external CDN

I am building a portfolio to show work I have created.我正在建立一个作品集来展示我创作的作品。 It includes some small projects from FreeCodeCamp that are built according to test specs.它包括一些来自 FreeCodeCamp 的根据测试规范构建的小项目。 I would like to include the tests so that someone looking at the code can see the tests.我想包括测试,以便查看代码的人可以看到测试。

When you embed the cdn script in your index.html file in a react app hosted anywhere, they work fine (even though React doesn't want outside forces manipulating the DOM).当您将 cdn 脚本嵌入到您的 index.html 文件中托管在任何地方的 react 应用程序中时,它们可以正常工作(即使 React 不希望外力操纵 DOM)。 This will install a hamburger menu that can be exploded and you can choose which tests you would like to run.这将安装一个可以展开的汉堡菜单,您可以选择要运行的测试。 Note that this hamburger is always displayed.请注意,始终显示此汉堡包。

However, I don't want to always have this hamburger menu because some of what I want to display is how the page is styled.但是,我不想总是拥有这个汉堡菜单,因为我想要显示的一些内容是页面的样式。 I am trying to wait and insert the script tag until a button is pressed.我试图等待并插入脚本标签,直到按下按钮。

I can successfully display and remove the hamburger menu if I hit the add then remove buttons in order, but I know that I am doing this wrong when it comes to React design.如果我按顺序点击添加然后删除按钮,我可以成功显示和删除汉堡菜单,但我知道在 React 设计方面我做错了。 This is obvious by the fact that if you hit add to dom button 2 times in a row, then it won't be removed.这一点很明显,如果你连续两次点击 add to dom 按钮,它就不会被删除。 Also, I can run the test suite the first time.另外,我可以第一次运行测试套件。 But if remove the hamburger and add it back and try to run the tests, there are errors in the console.但是,如果删除汉堡包并将其添加回来并尝试运行测试,则控制台中会出现错误。

My question is: how can you properly add/remove a cdn script element (which will automatically add a dom element (hamburger), in a proper "React" design? I am thinking that solution lies with useEffect, useRef, or useState, but I am to "green" in React and when I research I still can't design a solution that works.我的问题是:如何正确添加/删除一个 cdn 脚本元素(它将自动添加一个 dom 元素(汉堡包),以适当的“反应”设计?我认为解决方案在于 useEffect、useRef 或 useState,但是我在 React 中是“绿色”的,当我研究时,我仍然无法设计一个有效的解决方案。

I put the code in a sandbox.我将代码放在沙箱中。 To recreate the problem, just click the show the hamburger button, then hide the hamburger button, then show again and try to run the tests.要重现问题,只需单击显示汉堡包按钮,然后隐藏汉堡包按钮,然后再次显示并尝试运行测试。 Or click show several times in a row.或连续点击数次显示。

Any tips on how to fix this would be appreciated.任何有关如何解决此问题的提示将不胜感激。 This problem way exceeds my little experience.这个问题方式超出了我的一点经验。

Link to Code Sandbox 链接到代码沙箱

So I figured this out myself.所以我自己想出了这个。 In React, To safely make an element loaded into the DOM via external CDN appear and disappear was as simple as CSS.在 React 中,通过外部 CDN 安全地使加载到 DOM 的元素出现和消失就像 CSS 一样简单。

I just added the item to the DOM with the CDN script tag.我刚刚使用 CDN 脚本标签将该项目添加到 DOM。 Then I could change either display:none or visibility: hidden .然后我可以更改display:nonevisibility: hidden

The trick was loading the CDN script in first, then hiding it.诀窍是先加载 CDN 脚本,然后将其隐藏。

I was encountering a problem because I was trying the faulty approach of adding the CDN script tag, and removing, and adding again.我遇到了一个问题,因为我正在尝试添加 CDN 脚本标签、删除并再次添加的错误方法。

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

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