1
- import React , { Fragment } from 'react'
1
+ import React , { Fragment , useEffect , useState } from 'react'
2
2
import { BrowserRouter as Router , Route } from 'react-router-dom'
3
3
4
4
import Header from './Header'
@@ -14,13 +14,10 @@ import conf from '../conf'
14
14
15
15
import '../styles/App.styl'
16
16
17
- class App extends React . Component {
18
- constructor ( props ) {
19
- super ( props )
20
- this . state = {
21
- isLoading : true ,
22
- }
17
+ const App = ( ) => {
18
+ const [ isLoading , setIsLoading ] = useState ( true )
23
19
20
+ useEffect ( ( ) => {
24
21
fetch ( process . env . RUNTIME_CONF_URI )
25
22
. then ( ( response ) => {
26
23
if ( response . status === 404 ) {
@@ -29,39 +26,37 @@ class App extends React.Component {
29
26
return response . json ( )
30
27
} )
31
28
. then ( json => Object . assign ( conf , json ) )
32
- . then ( ( ) => this . setState ( { isLoading : false } ) )
29
+ . then ( ( ) => setIsLoading ( false ) )
33
30
34
31
// AceEditor's modes (aka syntaxes) are pretty heavy, and since they are
35
32
// not essential, we better download them asynchronously when the app is
36
33
// loaded and ready to be used.
37
34
for ( const syntax of process . env . SYNTAXES ) {
38
35
import ( `brace/mode/${ syntax } .js` )
39
36
}
40
- }
37
+ } , [ ] )
41
38
42
- render ( ) {
43
- if ( this . state . isLoading ) {
44
- return < Spinner />
45
- }
46
-
47
- return (
48
- < Router >
49
- < Fragment >
50
- < Header key = "header" />
51
- < div className = "content" key = "content" >
52
- < Sidebar />
53
- < main className = "main" >
54
- < Route exact path = "/" component = { NewSnippet } />
55
- < Route exact path = "/recent" component = { RecentSnippets } />
56
- < Route exact path = "/:id(\d+)" component = { Snippet } />
57
- < Route exact path = "/about" component = { About } />
58
- < Route exact path = "/sign-in" component = { SignIn } />
59
- </ main >
60
- </ div >
61
- </ Fragment >
62
- </ Router >
63
- )
39
+ if ( isLoading ) {
40
+ return < Spinner />
64
41
}
42
+
43
+ return (
44
+ < Router >
45
+ < Fragment >
46
+ < Header />
47
+ < div className = "content" >
48
+ < Sidebar />
49
+ < main className = "main" >
50
+ < Route exact path = "/" component = { NewSnippet } />
51
+ < Route exact path = "/recent" component = { RecentSnippets } />
52
+ < Route exact path = "/:id(\d+)" component = { Snippet } />
53
+ < Route exact path = "/about" component = { About } />
54
+ < Route exact path = "/sign-in" component = { SignIn } />
55
+ </ main >
56
+ </ div >
57
+ </ Fragment >
58
+ </ Router >
59
+ )
65
60
}
66
61
67
62
export default App
0 commit comments