如何在React网站中使用媒体小部件

问题描述 投票:2回答:2

我找到了一个惊人的链接Here,为Medium帖子创建小部件。

不幸的是我无法在react网站上使用该代码

示例(随机媒体作者):

<div id="medium-widget"></div>
    <script src="https://medium-widget.pixelpoint.io/widget.js"></script>
    <script>MediumWidget.Init({renderTo: '#medium-widget', params: {"resource":"https://medium.com/@sunilsandhu","postsPerLine":2,"limit":4,"picture":"big","fields":["description","author","claps","publishAt"],"ratio":"landscape"}})</script>

结果应该是这样的

enter image description here

我如何在React网站中使用该代码?

此外,我发现了这个here的Angular版本,但无法在React app中使用。

提前致谢

javascript angularjs reactjs
2个回答
2
投票

在index.html中

<script src="https://medium-widget.pixelpoint.io/widget.js"></script>
<script type="text/javascript">
  function mediumWidget(){
    MediumWidget.Init({renderTo: '#medium-widget', params: {"resource":"https://medium.com/@sunilsandhu","postsPerLine":2,"limit":4,"picture":"big","fields":["description","author","claps","publishAt"],"ratio":"landscape"}});
  }
</script>

在您的组件中

componentDidMount() {
  window.mediumWidget();
}
render() {
  return (<div id="medium-widget"></div>);
}

1
投票

只需在MediumWidget.Init生命周期运行componentDidMount代码或使用useEffect钩子运行它。

© www.soinside.com 2019 - 2024. All rights reserved.