From 906b53dafd871a8877485d01f1ded2215c416d49 Mon Sep 17 00:00:00 2001 From: Bret Little Date: Fri, 3 Dec 2021 09:42:30 -0500 Subject: [PATCH] fix: move to a singleton for tracking Helmet instances This resolves an issue where multiple HelmetProviders on the same page don't know about each other. --- jest.setup.js | 2 ++ src/HelmetData.js | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/jest.setup.js b/jest.setup.js index c6f4d206..847ef158 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -2,6 +2,7 @@ import 'raf/polyfill'; import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; import ReactDOM from 'react-dom'; +import { clearInstances } from './src/HelmetData'; configure({ adapter: new Adapter() }); @@ -40,4 +41,5 @@ beforeEach(() => { afterEach(() => { ReactDOM.unmountComponentAtNode(mount); + clearInstances(); }); diff --git a/src/HelmetData.js b/src/HelmetData.js index 8f9a19a0..a8e50bce 100644 --- a/src/HelmetData.js +++ b/src/HelmetData.js @@ -1,31 +1,31 @@ import mapStateOnServer from './server'; -export default class HelmetData { - instances = []; +const instances = []; + +export function clearInstances() { + instances.length = 0; +} +export default class HelmetData { value = { setHelmet: serverState => { this.context.helmet = serverState; }, helmetInstances: { - get: () => this.instances, + get: () => instances, add: instance => { - this.instances.push(instance); + instances.push(instance); }, remove: instance => { - const index = this.instances.indexOf(instance); - this.instances.splice(index, 1); + const index = instances.indexOf(instance); + instances.splice(index, 1); }, }, }; - constructor(context, instances) { + constructor(context) { this.context = context; - if (instances) { - this.instances = instances; - } - if (!HelmetData.canUseDOM) { context.helmet = mapStateOnServer({ baseTag: [],