Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate the Twitter widget into Reactjs?

Tags:

I want to add the Twitter widget into React, but I don't know where to start or how to do it. I am very new to React JS.

Here is the HTML version of the code:

<div class="Twitter">   <a class="twitter-timeline" href="https://twitter.com/<%= @artist.twitter %>" data-widget-id="424584924285239296" data-screen-name='<%= @artist.twitter %>'>Tweets by @<%= @artist.twitter %></a>   <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> </div> 

And here is what I have so far:

React.DOM.div   className: 'Twitter'   children: [     React.DOM.a       className: 'twitter-timeline'       href: "https://twitter.com/" + artist.twitter       'data-widget-id': "424584924285239296"        'data-screen-name': artist.twitter       children: 'Tweets by ' + artist.twitter     React.DOM.script       children: ...   ] 

I was planning to add the script where the dots (...) are, but that doesn't work. Thank you for your help.

like image 376
Finch Avatar asked Aug 10 '14 23:08

Finch


People also ask

How do I integrate twitter with my react app?

Register the app via the Twitter Development Portal. https://developer.twitter.com and go to the Developer Portal. Go to a project (or create one if you don't have one yet). Go to + Add App > Create New App Instead. Name your app appropriately.

Can you embed a widget on twitter?

Twitter no longer allows this, you must use the twitter embed code generator on their publish.twitter.com site to select your widget embed code. Once on the site, you can select your customized desired widget based on the examples provided in the drop downs.


1 Answers

First load Widget JS in your index.html(before your React scripts). Then in your component, simply do the following. The widget JS will rescan the DOM.

componentDidMount: function() {   twttr.widgets.load() } 

See: https://dev.twitter.com/web/javascript/initialization

like image 131
Raja Rao Avatar answered Oct 23 '22 00:10

Raja Rao