Skip to content

Commit 665347f

Browse files
committed
Refactor
1 parent efd50be commit 665347f

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function App() {
5656
>
5757
<LanguageContext.Provider
5858
value={{
59+
currentLanguage: currentLang,
5960
setCurrentLanguageOptionContext: (value) => {
6061
setCurrentLang(value)
6162
},

src/containers/global/LanguageSelector.tsx

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { Select } from 'antd'
2-
import { useContext, useEffect, useState } from 'react'
2+
import { useContext } from 'react'
33
import { useDispatch } from 'react-redux'
44
import LanguageContext from '../../contexts/LanguageContext'
55
import { emitRootKeyChanged } from '../../redux/actions/GlobalActions'
66
import {
7-
getCurrentLanguageOption,
87
languagesOptions,
98
setCurrentLanguageOption,
109
} from '../../utils/Language'
@@ -14,33 +13,25 @@ interface LanguageSelectorProps {
1413
}
1514

1615
const LanguageSelector: React.FC<LanguageSelectorProps> = ({ forceReload }) => {
17-
const [currentLanguage, setCurrentLanguage] = useState(
18-
getCurrentLanguageOption().value
19-
)
20-
21-
const { setCurrentLanguageOptionContext } = useContext(LanguageContext)
16+
const { currentLanguage, setCurrentLanguageOptionContext } =
17+
useContext(LanguageContext)
2218

2319
const dispatch = useDispatch()
2420

2521
const handleChange = (value: string) => {
2622
const langOption = setCurrentLanguageOption(value)
2723
setCurrentLanguageOptionContext(langOption)
28-
setCurrentLanguage(value)
2924
dispatch(emitRootKeyChanged())
3025
if (forceReload) {
3126
window.location.reload()
3227
}
3328
}
3429

35-
useEffect(() => {
36-
setCurrentLanguage(getCurrentLanguageOption().value)
37-
}, [])
38-
3930
return (
4031
<Select
4132
style={{ width: 150 }}
4233
options={languagesOptions}
43-
value={currentLanguage}
34+
value={currentLanguage.value}
4435
onChange={handleChange}
4536
/>
4637
)

src/contexts/LanguageContext.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createContext } from 'react'
2-
import { LanguageOption } from '../utils/Language'
2+
import { getCurrentLanguageOption, LanguageOption } from '../utils/Language'
33

44
const LanguageContext = createContext({
5+
currentLanguage: getCurrentLanguageOption(),
56
setCurrentLanguageOptionContext: (value: LanguageOption) => {},
67
})
78

0 commit comments

Comments
 (0)