From 03b59a2f8538bf47040c5e97f1db44054615950d Mon Sep 17 00:00:00 2001 From: Jamie Greeff Date: Thu, 4 Feb 2016 10:08:55 +0000 Subject: [PATCH] Throw a useful error when injecting tap event plugin more than once 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. --- src/injectTapEventPlugin.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/injectTapEventPlugin.js b/src/injectTapEventPlugin.js index 9d057c975de58..dd5c464280abf 100644 --- a/src/injectTapEventPlugin.js +++ b/src/injectTapEventPlugin.js @@ -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) }); };