diff --git a/source/src/App.tsx b/source/src/App.tsx index c499609..2c6618a 100644 --- a/source/src/App.tsx +++ b/source/src/App.tsx @@ -1,4 +1,4 @@ -import React from "react" +import React, { useEffect, useState } from "react" import { Routes, Route } from "react-router-dom" import styled, { ThemeProvider, @@ -249,25 +249,13 @@ const StyledContentContainer = styled.div` margin-top: 5rem; ` -interface AppProps {} +const App = () => { + const [isLoading, setIsLoading] = useState(true) + const [currentTheme, setCurrentTheme] = useState( + storage.getItem("theme") || "dark" // get theme from storage and set to "dark" mode if not set already + ) -interface AppState { - isLoading: boolean - currentTheme: string - currentLanguage: string -} - -export default class App extends React.Component { - constructor(props: AppProps) { - super(props) - this.state = { - isLoading: true, - currentTheme: storage.getItem("theme") || "dark", // get theme from storage and set to "dark" mode if not set already - currentLanguage: storage.getItem("lang") || "en", // get language from storage and set to "en" if not set already - } - } - - componentDidMount() { + useEffect(() => { // show loading screen until all fonts are loaded. // Experimental feature. Not fully supported on all browsers (IE, I'm looking at you). // https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet @@ -275,75 +263,63 @@ export default class App extends React.Component { // checks if document.fonts.onloadingdone is supported on the browser if (typeof document.fonts.onloadingdone != undefined) { document.fonts.onloadingdone = () => { - this.setState({ isLoading: false }) + setIsLoading(false) } } else { - this.setState({ isLoading: false }) + setIsLoading(false) } - } + }, []) - componentDidUpdate(_: AppProps, prevState: AppState) { - if (this.state.currentTheme !== prevState.currentTheme) { - // save theme when it is changed - storage.setItem("theme", this.state.currentTheme) - } - - if (this.state.currentLanguage !== prevState.currentLanguage) { - // save language when it is changed - storage.setItem("lang", this.state.currentLanguage) - } - } - - render() { - if (isIE) - return ( - - Internet Explorer is not supported. - - ) + useEffect(() => { + // save theme when it is changed + storage.setItem("theme", currentTheme) + }, [currentTheme]) + if (isIE) return ( - - this.setState({ currentTheme: setThemeTo }), // make setTheme function available in other components - }} - > - - - - - - - - - - - - - - - {this.state.isLoading ? ( - - ) : ( - - } - /> - } /> - } /> - } /> - } /> - - )} - -