Skip to content

Commit

Permalink
refactor(react-hooks): update callback usage in usePortalNode
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblack committed Sep 4, 2019
1 parent 9ecb3b6 commit 1d3e098
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
10 changes: 5 additions & 5 deletions packages/react-hooks/src/useAnnouncer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');

Expand All @@ -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.
Expand Down
14 changes: 2 additions & 12 deletions packages/react-hooks/src/usePortalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1d3e098

Please sign in to comment.