[英]How to add Pixlee Social Feed to a React application?
I have created a social feed for my Instagram using Pixlee and got a sample to use with my page looking like this:我使用 Pixlee 为我的 Instagram 创建了一个社交提要,并获得了一个示例用于我的页面,如下所示:
<div id="pixlee_container"></div>
<script type="text/javascript">window.PixleeAsyncInit = function() {Pixlee.init({apiKey:'ThisIsASecretApiKey'});Pixlee.addSimpleWidget({widgetId:'32861'});};</script>
<script src="//instafeed.assets.pxlecdn.com/assets/pixlee_widget_1_0_0.js"></script>
How can I incorporate this into a react page?如何将其合并到反应页面中? How can I load and execute whats within the script tags in a React context?
如何在 React 上下文中加载和执行脚本标签中的内容?
EDIT:编辑:
I found that I can use <Helmet>
to include scripts within a React component like this:我发现我可以使用
<Helmet>
在 React 组件中包含脚本,如下所示:
<Helmet>
<script type="text/javascript">window.PixleeAsyncInit = function() {Pixlee.init({apiKey:'ThisIsASecretApiKey'});Pixlee.addSimpleWidget({widgetId:'32861'});};</script>
<script src="//instafeed.assets.pxlecdn.com/assets/pixlee_widget_1_0_0.js"></script>
</Helmet>
Doing this I get an error for the first script tag saying:这样做我得到一个错误的第一个脚本标签说:
C:\utveckling\blog\src\pages\instagram.tsx: Unexpected token, expected "}" (18:122)
I found a solution by putting the script parts within a useEffect
tag like this:我通过将脚本部分放在
useEffect
标记中找到了一个解决方案,如下所示:
const InstagramFeed = (): JSX.Element => {
// Init Pixlee Social Feed
useEffect(() => {
window.PixleeAsyncInit = function() {
Pixlee.init({ apiKey: "ThisIsASecretApiKey" })
Pixlee.addSimpleWidget({ widgetId: "32861" })
}
const scriptTag = document.createElement("script")
scriptTag.src =
"//instafeed.assets.pxlecdn.com/assets/pixlee_widget_1_0_0.js"
document.body.appendChild(scriptTag)
}, [])
return (
<>
<div id="pixlee_container" />
</>
)
}
You should add the scripts to your index.html
that is in your public folder and your div
whatever component you want.您应该将脚本添加到您的公共文件夹中的
index.html
和您div
的任何组件中。 See this CodeSandbox: https://codesandbox.io/s/mystifying-tdd-3x6pn?file=/public/index.html:587-606请参阅此 CodeSandbox: https://codesandbox.io/s/mystifying-tdd-3x6pn?file=/public/index.html:587-606
If you set your key in the script function, it should work.如果您在脚本 function 中设置密钥,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.