Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 828f05f

Browse files
committed
Trying with ref
1 parent 810b9e1 commit 828f05f

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

packages/gatsby/cache-dir/navigation.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ function init() {
161161
class RouteAnnouncer extends React.Component {
162162
constructor(props) {
163163
super(props)
164-
this.state = { announcement: `` }
164+
this.announcementRef = React.createRef()
165165
}
166166

167-
componentDidUpdate(prevProps) {
167+
componentDidUpdate(prevProps, nextProps) {
168168
requestAnimationFrame(() => {
169169
let pageName = `new page at ${this.props.location.pathname}`
170170
if (document.title) {
@@ -173,20 +173,18 @@ class RouteAnnouncer extends React.Component {
173173
const pageHeadings = document
174174
.getElementById(`gatsby-focus-wrapper`)
175175
.getElementsByTagName(`h1`)
176-
if (pageHeadings) {
176+
if (pageHeadings && pageHeadings.length) {
177177
pageName = pageHeadings[0].textContent
178178
}
179-
let newAnnouncement = `Navigated to ${pageName}`
180-
if (this.state.announcement !== newAnnouncement) {
181-
this.setState({
182-
announcement: newAnnouncement,
183-
})
179+
const newAnnouncement = `Navigated to ${pageName}`
180+
const oldAnnouncement = this.announcementRef.current.innerText
181+
if (oldAnnouncement !== newAnnouncement) {
182+
this.announcementRef.current.innerText = newAnnouncement
184183
}
185184
})
186185
}
187186

188187
render() {
189-
const { announcement } = this.state
190188
return (
191189
<div
192190
id="gatsby-announcer"
@@ -203,9 +201,8 @@ class RouteAnnouncer extends React.Component {
203201
role="alert"
204202
aria-live="assertive"
205203
aria-atomic="true"
206-
>
207-
{announcement}
208-
</div>
204+
ref={this.announcementRef}
205+
></div>
209206
)
210207
}
211208
}

0 commit comments

Comments
 (0)