首页 > 解决方案 > Reactjs 和脚本

问题描述

我正在用 reactJS 建立一个博客。然而,危险的SetInnerHTML 不会在博客文章中呈现脚本。

这些博客文章包含 twitter 小部件或 github 嵌入等

有没有办法做到这一点?

标签: javascriptreactjs

解决方案


您始终可以在 index.html 文件中包含这些脚本。如果你想有条件地添加这些脚本文件,你可以这样做。

var script = document.createElement("script");
    script.type = "text/javascript";
if (window.matchMedia("(max-width: 568px)").matches) {
    script.src = "//path/to/mobile-users.js";
} else {
    script.src = "//path/to/desktop-users.js";
}
document.body.appendChild(script);

推荐阅读