Global React does not do well with AMD React

I get strange weird behavior when rendering a component using a reloaded AMD React when a global one Reactalready exists on the page. Component click events are fired when they should not be.

A look at the DOM suggests that this is due to several React instances (one global, one AMD in my case) that are not familiar with each other, but this creates a problem when loading the AMD module at runtime, which depends on React, on the page in which also includes React.

How can I resolve this collision?

Play

I can make a component like this:

var ButtonComponent = React.createClass({
  onButtonClick: function(){
    alert(this.props.data + ' click event fired');
  },
  render: function() {
    return React.DOM.button({onClick: this.onButtonClick}, this.props.data);
  }
});

(function(){ // create vanilla

  var ButtonList = React.createClass({
    render: function() {
      return React.DOM.div({}, React.createElement(ButtonComponent, {data: this.props.data}));
    }
  });

  React.render(React.createElement(ButtonList, {data: 'button that was loaded by the page'}), document.getElementById('page-load-target'));

})();

jsbin

, React, , click :

// .... as above ....

(function(){ // create using amd

  require.config({
    paths: {
      'react': '//fb.me/react-with-addons-0.12.2.min'
    }
  });

  window.setTimeout(function(){ 
    require(['react'], function(ReactFromAmd){ 
      ReactFromAmd.render(ReactFromAmd.createElement(ButtonComponent, {data: 'button that was loaded by AMD'}), document.getElementById('amd-load-target'));
    });
  }, 1000)

})();

jsbin

React ( ReactFromAmd), . jsbin

+4
2

s ( React) ButtonComponent, , ReactFromAmd.createElement, , DOM - .

, AMD , , / React .

( , - )

0

All Articles