M
M
Maxim Nikulin2016-07-25 18:38:05
JavaScript
Maxim Nikulin, 2016-07-25 18:38:05

How to connect social media share buttons in React?

Hello. There is an isomorphic SPA. It is necessary to connect share-buttons of social networks to some pages. How to do it? It would be great with an example. Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
evilandfox, 2016-07-26
@max_nikulin

The trick is that for such components initialized by javascript, you need to do initialization in componentDidMont through access using ref:
codepen.io/evilandfox/pen/jAxJxv

<script type="text/javascript" src="http://vk.com/js/api/share.js?93" charset="windows-1251"></script>
<script>window.twttr = (function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
  if (d.getElementById(id)) return t;
  js = d.createElement(s);
  js.id = id;
  js.src = "https://platform.twitter.com/widgets.js";
  fjs.parentNode.insertBefore(js, fjs);
 
  t._e = [];
  t.ready = function(f) {
    t._e.push(f);
  };
  return t;
}(document, "script", "twitter-wjs"));</script>
<div id="main"></div>

var Component = React.createClass({
  componentDidMount: function(){
    this.refs.vk.innerHTML = VK.Share.button(false, {type: "round", text: "Сохранить"});
    twttr.widgets.createShareButton(
      'https://dev.twitter.com/',
      this.refs.tw,
      {
        text: 'Hello World'
      });
  },
  render: function(){
    return (
      <div>
        <h4>Поделиться:</h4>
        <div ref="vk"></div>
        <div ref="tw"></div>
      </div>
    );
  }
});

ReactDOM.render(<Component/>, document.getElementById("main"));

M
merss, 2017-04-02
@merss

How do you do it? I get an error "error 'VK' is not defined"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question