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

Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 797fd5c

Browse files
authored
farrah/fix navbar language switcher re-render (#2488)
* fix rendering of language switcher * fix setting of client information * refactor code * added a helper function for callback reference
1 parent 4df855f commit 797fd5c

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/common/utility.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useRef } from 'react'
12
import { navigate } from 'gatsby'
23
import Cookies from 'js-cookie'
34
import extend from 'extend'
@@ -368,6 +369,16 @@ export const getBaseRef = (ref) => {
368369
return ref?.current?.base?.style ? ref?.current?.base : ref?.current
369370
}
370371

372+
export const useCallbackRef = (callback) => {
373+
const callback_ref = useRef()
374+
375+
useEffect(() => {
376+
callback_ref.current = callback
377+
}, [callback])
378+
379+
return callback_ref
380+
}
381+
371382
const uk_subdomain_countries = ['gb']
372383
const eu_subdomain_countries = ['nl']
373384

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import { useState, useEffect } from 'react'
2-
import { getClientInformation, getDomain } from 'common/utility'
2+
import { getClientInformation, getDomain, useCallbackRef } from 'common/utility'
33

44
export const useClientInformation = () => {
55
const [client_information, setClientInformation] = useState(false)
6-
const [current_client_information, setCurrentClientInformation] = useState(false)
6+
7+
const setCurrentClientInformation = () => {
8+
const current_client_information = getClientInformation(getDomain())
9+
const is_client_information_updated =
10+
current_client_information &&
11+
JSON.stringify(current_client_information) != JSON.stringify(client_information)
12+
13+
if (is_client_information_updated) setClientInformation(current_client_information)
14+
}
15+
16+
const callback_ref = useCallbackRef(setCurrentClientInformation)
717

818
useEffect(() => {
919
const cookie_interval = setInterval(() => {
10-
setCurrentClientInformation(getClientInformation(getDomain()))
20+
callback_ref.current()
1121
}, 1000)
22+
1223
return () => clearInterval(cookie_interval)
1324
}, [])
1425

15-
useEffect(() => {
16-
if (current_client_information) {
17-
if (JSON.stringify(current_client_information) != JSON.stringify(client_information)) {
18-
setClientInformation(current_client_information)
19-
}
20-
} else {
21-
setClientInformation(false)
22-
}
23-
}, [current_client_information])
24-
2526
return client_information
2627
}

0 commit comments

Comments
 (0)