From 1d3e0984633359ec0457fba1331d91a37d195691 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Wed, 4 Sep 2019 15:36:43 -0500 Subject: [PATCH] refactor(react-hooks): update callback usage in usePortalNode --- packages/react-hooks/src/useAnnouncer.js | 10 +++++----- packages/react-hooks/src/usePortalNode.js | 14 ++------------ 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/packages/react-hooks/src/useAnnouncer.js b/packages/react-hooks/src/useAnnouncer.js index 0f752c9260aa..6f6a1b46f94d 100644 --- a/packages/react-hooks/src/useAnnouncer.js +++ b/packages/react-hooks/src/useAnnouncer.js @@ -16,11 +16,7 @@ import { usePortalNode } from './usePortalNode'; * messages sent is important. */ export function useAnnouncer() { - const node = usePortalNode('carbon-announcer', node => { - if (!node.classList.contains('bx--visually-hidden')) { - node.classList.add('bx--visually-hidden'); - } - }); + const node = usePortalNode('carbon-announcer'); const [mode, updateMode] = useState('polite'); const [announcement, updateAnnouncement] = useState(''); @@ -34,6 +30,10 @@ export function useAnnouncer() { return; } + if (!node.classList.contains('bx--visually-hidden')) { + node.classList.add('bx--visually-hidden'); + } + // In this effect, we'll need to setup the `#carbon-announcer` node with two // corresponding announcement nodes if they do not exist already. If they // already exist, then we can reuse them. diff --git a/packages/react-hooks/src/usePortalNode.js b/packages/react-hooks/src/usePortalNode.js index a326a722450e..1570372f7382 100644 --- a/packages/react-hooks/src/usePortalNode.js +++ b/packages/react-hooks/src/usePortalNode.js @@ -5,28 +5,18 @@ * LICENSE file in the root directory of this source tree. */ -import { useEffect, useRef, useState } from 'react'; +import { useEffect, useState } from 'react'; /** * @param {string?} id - * @param {Function?} callback - run side-effects on the created node */ -export function usePortalNode(id, callback) { +export function usePortalNode(id) { const [portalNode, setPortalNode] = useState(null); - const savedCallback = useRef(callback); - - useEffect(() => { - savedCallback.current = callback; - }, [callback]); useEffect(() => { const [node, cleanup] = findOrCreateRoot(id); setPortalNode(node); - if (savedCallback.current) { - savedCallback.current(node); - } - return () => { cleanup(); setPortalNode(null);