From 9b0e317e0a614be88afd7b85c49ae1446120f3dc Mon Sep 17 00:00:00 2001 From: Roderick Hsiao Date: Tue, 27 Apr 2021 13:32:13 -0700 Subject: [PATCH] Prevent setup plugins run multiple times (#326) --- package.json | 2 +- src/core/ReactI13n.js | 5 +++++ src/core/setupI13n.jsx | 20 +++++++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 51df8b08..44e1dc29 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/core/ReactI13n.js b/src/core/ReactI13n.js index 6254c65b..dfcf6bce 100644 --- a/src/core/ReactI13n.js +++ b/src/core/ReactI13n.js @@ -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 diff --git a/src/core/setupI13n.jsx b/src/core/setupI13n.jsx index b523b3d8..41e588b1 100644 --- a/src/core/setupI13n.jsx +++ b/src/core/setupI13n.jsx @@ -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'; @@ -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();