Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 67cde58

Browse files
authored
Merge pull request #160 from geotrev/app/refs-update
Convert createRef to preferred useRef
2 parents 05d0270 + 59123ca commit 67cde58

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

app/components/Article/Article.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ export default function Article(props) {
1414
COMPONENTS.forEach(Component => Component.stop())
1515
}
1616

17+
const onMountOnly = []
1718
useEffect(() => {
1819
Prism.highlightAll()
1920
COMPONENTS.forEach(Component => Component.start())
2021
setDomIsLoaded(true)
2122

2223
return componentUnmountFunction
23-
}, [])
24+
}, onMountOnly)
2425

2526
return (
2627
<article

app/components/PageHeader/PageHeader.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import React, { createRef, useState, useEffect } from "react"
1+
import React, { useRef, useState, useEffect } from "react"
22
import { withLastLocation } from "react-router-last-location"
33
import PropTypes from "prop-types"
44

55
import { FOCUSABLE_TABINDEX, UNFOCUSABLE_TABINDEX } from "./constants"
66

77
const PageHeader = props => {
88
const [tabIndex, setTabIndex] = useState(FOCUSABLE_TABINDEX)
9-
const headerRef = createRef()
9+
const headerRef = useRef(null)
10+
const onMountOnly = []
1011

1112
useEffect(() => {
1213
if (props.lastLocation) {
1314
headerRef.current.focus()
1415
} else {
1516
setTabIndex(UNFOCUSABLE_TABINDEX)
1617
}
17-
}, [])
18+
}, onMountOnly)
1819

1920
const handleBlur = () => {
2021
if (tabIndex === UNFOCUSABLE_TABINDEX) return
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { useEffect } from "react"
22

33
export default function ScrollUpOnMount() {
4+
const onMountOnly = []
5+
46
useEffect(() => {
57
window.scrollTo(0, 0)
6-
}, [])
8+
}, onMountOnly)
79

810
return null
911
}

app/components/SideNav/SideNav.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ export default function SideNav() {
3333
Accordions.stop()
3434
}
3535

36+
const onMountOnly = []
3637
useEffect(() => {
3738
Accordions.start()
3839
window.addEventListener("resize", handleMenuVisibility)
3940
setMenuIsOpen(isLargerThanCollapseWidth)
4041

4142
return componentUnmountFunction
42-
}, [])
43+
}, onMountOnly)
4344

45+
const updateConditions = [menuIsOpen]
4446
useEffect(() => {
4547
Accordions.stop()
4648
Accordions.start()
47-
}, [menuIsOpen])
49+
}, updateConditions)
4850

4951
// set up handlers and other helper methods
5052

app/layouts/Main/Main.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ import "./styles.scss"
1414
import { FOCUSABLE_TABINDEX, UNFOCUSABLE_TABINDEX } from "./constants"
1515

1616
export default function Main() {
17+
const headerRef = React.useRef(null)
18+
const mainRef = React.useRef(null)
1719
const Attributes = {
1820
TABINDEX: "tabindex",
1921
}
22+
const getHeaderTabIndex = () => headerRef.current.getAttribute(Attributes.TABINDEX)
23+
const getMainTabIndex = () => mainRef.current.getAttribute(Attributes.TABINDEX)
2024

21-
const headerRef = React.createRef()
22-
const mainRef = React.createRef()
2325
const componentUnmountFunction = () => {
2426
ContextUtil.disableFocusOutline()
2527
}
2628

29+
const onMountOnly = []
2730
useEffect(() => {
2831
ContextUtil.enableFocusOutline()
2932
return componentUnmountFunction
30-
}, [])
33+
}, onMountOnly)
3134

3235
const handleRefocusClick = ref => {
3336
ref.current.setAttribute(Attributes.TABINDEX, FOCUSABLE_TABINDEX)
3437
ref.current.focus()
3538
}
3639

37-
const getHeaderTabIndex = () => headerRef.current.getAttribute(Attributes.TABINDEX)
38-
const getMainTabIndex = () => mainRef.current.getAttribute(Attributes.TABINDEX)
39-
4040
const handleHeaderBlur = () => {
4141
if (getHeaderTabIndex() === UNFOCUSABLE_TABINDEX) return
4242
headerRef.current.removeAttribute(Attributes.TABINDEX)

app/pages/Home/Home.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ export default function Home() {
4141
ANIMATION_DATA.forEach(instance => instance.animation.destroy())
4242
}
4343

44+
const onMountOnly = []
4445
useEffect(() => {
4546
Prism.highlightAll()
4647
loadAnimations()
4748

4849
return componentUnmountFunction
49-
}, [])
50+
}, onMountOnly)
5051

5152
const renderAnimations = () => {
5253
return ANIMATION_DATA.map(animation => {

0 commit comments

Comments
 (0)