@@ -161,10 +161,10 @@ function init() {
161
161
class RouteAnnouncer extends React . Component {
162
162
constructor ( props ) {
163
163
super ( props )
164
- this . state = { announcement : `` }
164
+ this . announcementRef = React . createRef ( )
165
165
}
166
166
167
- componentDidUpdate ( prevProps ) {
167
+ componentDidUpdate ( prevProps , nextProps ) {
168
168
requestAnimationFrame ( ( ) => {
169
169
let pageName = `new page at ${ this . props . location . pathname } `
170
170
if ( document . title ) {
@@ -173,20 +173,18 @@ class RouteAnnouncer extends React.Component {
173
173
const pageHeadings = document
174
174
. getElementById ( `gatsby-focus-wrapper` )
175
175
. getElementsByTagName ( `h1` )
176
- if ( pageHeadings ) {
176
+ if ( pageHeadings && pageHeadings . length ) {
177
177
pageName = pageHeadings [ 0 ] . textContent
178
178
}
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
184
183
}
185
184
} )
186
185
}
187
186
188
187
render ( ) {
189
- const { announcement } = this . state
190
188
return (
191
189
< div
192
190
id = "gatsby-announcer"
@@ -203,9 +201,8 @@ class RouteAnnouncer extends React.Component {
203
201
role = "alert"
204
202
aria-live = "assertive"
205
203
aria-atomic = "true"
206
- >
207
- { announcement }
208
- </ div >
204
+ ref = { this . announcementRef }
205
+ > </ div >
209
206
)
210
207
}
211
208
}
0 commit comments