Skip to content

Commit

Permalink
Prevent setup plugins run multiple times (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
roderickhsiao authored Apr 27, 2021
1 parent e7980aa commit 9b0e317
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-i13n",
"description": "React I13n provides a performant and scalable solution to application instrumentation.",
"version": "3.0.0-alpha.5",
"version": "3.0.0-alpha.6",
"main": "index.js",
"module": "index.es.js",
"repository": {
Expand Down
5 changes: 5 additions & 0 deletions src/core/ReactI13n.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class ReactI13n {
this._eventsQueues[plugin.name] = new EventsQueue(plugin);
}

cleanUpPlugins() {
this._eventsQueues = {};
this._plugins = {};
}

/**
* Get handlers from all plugin by event name
* @method getEventHandlers
Expand Down
20 changes: 13 additions & 7 deletions src/core/setupI13n.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

import React, { useMemo } from 'react';
import React, { useMemo, useEffect } from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';

import getDisplayName from '../utils/getDisplayName';
Expand Down Expand Up @@ -53,12 +53,18 @@ function setupI13n(Component, options = {}, plugins = []) {
executeI13nEvent: executeEvent
} = useReactI13nRoot(options);

if (reactI13n) {
plugins.forEach((plugin) => {
reactI13n.plug(plugin);
});
reactI13n.createRootI13nNode();
}
useEffect(() => {
if (reactI13n) {
plugins.forEach((plugin) => {
reactI13n.plug(plugin);
});
reactI13n.createRootI13nNode();
}

return () => {
reactI13n?.cleanUpPlugins();
};
}, [reactI13n, plugins]);

const parentI13nNode = reactI13n?.getRootI13nNode();

Expand Down

0 comments on commit 9b0e317

Please sign in to comment.