Skip to content

Commit

Permalink
Throw a useful error when injecting tap event plugin more than once
Browse files Browse the repository at this point in the history
This will prevent people from receiving a cryptic error message
from React about two plugins with the same name being injected.

injectTapEventPlugin() should only be called once per application,
and ideally at the entry point.
  • Loading branch information
madjam002 committed Feb 4, 2016
1 parent 6f55a84 commit 03b59a2
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/injectTapEventPlugin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
var defaultClickRejectionStrategy = require("./defaultClickRejectionStrategy");
var invariant = require('fbjs/lib/invariant');
var defaultClickRejectionStrategy = require('./defaultClickRejectionStrategy');

var alreadyInjected = false;

module.exports = function injectTapEventPlugin (strategyOverrides) {
strategyOverrides = strategyOverrides || {}
var shouldRejectClick = strategyOverrides.shouldRejectClick || defaultClickRejectionStrategy;

if (process.env.NODE_ENV !== 'production') {
invariant(
!alreadyInjected,
'injectTapEventPlugin(): Can only be called once per application lifecycle.\n\n\
It is recommended to call injectTapEventPlugin() just before you call \
ReactDOM.render(). If you are using an external library which calls injectTapEventPlugin() \
itself, please contact the maintainer as it shouldn\'t be called in library code and \
should be injected by the application.'
)
}

alreadyInjected = true;

require('react/lib/EventPluginHub').injection.injectEventPluginsByName({
"TapEventPlugin": require('./TapEventPlugin.js')(shouldRejectClick)
'TapEventPlugin': require('./TapEventPlugin.js')(shouldRejectClick)
});
};

0 comments on commit 03b59a2

Please sign in to comment.