2017-08-25 68 views
0

我想在我的反應頁面的特定位置包含linkedin follow按鈕。如何將下面的標籤呈現爲div?如何在ReactJS中呈現標記腳本

<script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: pt_BR</script> 
<script type="IN/FollowCompany" data-id="xxxxx"></script> 

謝謝。

+1

你不會渲染具有反應的腳本標記。只需將它們附加到您的html文件。 – trixn

+0

或者你可以看看https://github.com/leozdgao/react-async-script-loader – Purgatory

回答

0

您可以創建一個組件作出反應呈現腳本標記使用dangerouslySetInnerHTML插入作爲一個字符串的JavaScript代碼,就像這樣:

function LinkedInSnippet({ id }) { 
    return (
     <div> 
      <script 
       src="//platform.linkedin.com/in.js" 
       dangerouslySetInnerHTML={{ 
        __html: 'lang: pt_BR' 
       }} 
      /> 
      <script type="IN/FollowCompany" data-id={id} /> 
     </div> 
    ); 
} 

我假設你想要動態設置ID,所以我已經制作成可以作爲道具傳遞給組件。然後你可以使用這樣的組件:

<LinkedInSnippet id="xxxxxxx" />