Skip to content

Commit bbade9a

Browse files
authored
Merge pull request #139 from xsnippet/theme
[Hooks] Rewrote App component to hook
2 parents 7b548b1 + d9b0f10 commit bbade9a

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

src/components/App.jsx

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Fragment } from 'react'
1+
import React, { Fragment, useEffect, useState } from 'react'
22
import { BrowserRouter as Router, Route } from 'react-router-dom'
33

44
import Header from './Header'
@@ -14,13 +14,10 @@ import conf from '../conf'
1414

1515
import '../styles/App.styl'
1616

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)
2319

20+
useEffect(() => {
2421
fetch(process.env.RUNTIME_CONF_URI)
2522
.then((response) => {
2623
if (response.status === 404) {
@@ -29,39 +26,37 @@ class App extends React.Component {
2926
return response.json()
3027
})
3128
.then(json => Object.assign(conf, json))
32-
.then(() => this.setState({ isLoading: false }))
29+
.then(() => setIsLoading(false))
3330

3431
// AceEditor's modes (aka syntaxes) are pretty heavy, and since they are
3532
// not essential, we better download them asynchronously when the app is
3633
// loaded and ready to be used.
3734
for (const syntax of process.env.SYNTAXES) {
3835
import(`brace/mode/${syntax}.js`)
3936
}
40-
}
37+
}, [])
4138

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 />
6441
}
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+
)
6560
}
6661

6762
export default App

0 commit comments

Comments
 (0)