Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed Twitter widget to Reactjs?

Tags:

reactjs

Going to the twitter widget site (https://publish.twitter.com/) I am able to get a widget to add to my site. I am using the sample code to try to understand hot it works:

<a class="twitter-grid" href="https://twitter.com/TwitterDev/timelines/539487832448843776">National Park Tweets</a> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

If I just add this to an HTML page then the code works exactly as it should. It shows the grid with the respective tweets.

However, when adding this to ReactJS all I see is the fallback code, which merely provides a link to the twitter username.

I have tried using the following to no avail:

  <div dangerouslySetInnerHTML={{__html: "<a className='twitter-grid' href='https://twitter.com/TwitterDev/timelines/539487832448843776'>National Park Tweets</a> <script async src='https://platform.twitter.com/widgets.js' charset='utf-8'></script>"}}>
  </div>
like image 765
abrahamlinkedin Avatar asked Nov 29 '16 19:11

abrahamlinkedin


People also ask

Can you embed a widget on Twitter?

The good news is that you can just embed a Twitter widget on your website for that. This way, you can increase user engagement to your Twitter account and establish brand awareness. Plus, when you display a Twitter timeline widget, your customers can see it while checking out your product catalog or blog posts.

How do I get a Tweet embed code?

Find the tweet you'd like to embed. Click the downward arrow on the top-right of your tweet. Choose "Embed Tweet." Copy the code that appears and paste it into your website's HTML editor.


1 Answers

Check out this npm package: react-twitter-widgets. This seems to be accomplishing what you need out of box.

import { Tweet } from 'react-twitter-widgets'

ReactDOM.render(<Tweet tweetId='511181794914627584'/>, document.getElementById('root'))
like image 99
BradByte Avatar answered Nov 12 '22 03:11

BradByte