javascript - 使用 React 等前端技术在网页中显示嵌入的社交数据

标签 javascript reactjs

如何在reactjs中显示来自twitter的嵌入社交数据,该脚本标签已经用于样式化和渲染twitter并在数据库表中硬编码一些内容?在整体网站中,它工作得很好。在基于它的前端不起作用。

例如,以文本形式存储在数据库表中的内容:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">I just published “Making Sense of React Hooks” <a href=""></a></p>&mdash; Dan Abramov (@dan_abramov) <a href="https://twitter.com/dan_abramov/status/1057319198626594816?ref_src=twsrc%5Etfw">October 30, 2018</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum

此脚本 twitter 在使用 React 并将其周围的某些段落作为硬编码内容时不会加载默认的小部件布局。

最佳答案

问题出在脚本标记中。您可以直接将其添加到index.html或使用componentDidMount像这样

componentDidMount () {
     const script = document.createElement("script");

     script.src = "https://platform.twitter.com/widgets.js";
     script.async = true;

     document.body.appendChild(script);
}

检查这个Jsfiddle或者您可能想检查这个 npm package

关于javascript - 使用 React 等前端技术在网页中显示嵌入的社交数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53100490/

相关文章:

javascript - 如何识别在 react 中点击了哪个按钮?

javascript - react 路由和django url冲突

javascript - 如何使用 javascript 获取自定义 css var 的内联样式

javascript - JQuery - 删除具有特定类的类子元素

javascript - 带有 Firebase/Firestore 后端的双向无限滚动

javascript - 将图像上传到 GraphQL 服务器

javascript - 仅将静态样式分配给每个渲染组件一次

reactjs - 尝试 yarn start 时如何解决 web 重要错误?

javascript - 使图像适合图像

javascript - Async.forEach 并行运行,但它丢失了原始数组中的排序顺序