Skip to content

Commit d6a6dea

Browse files
refactor: Switch to hooks (preactjs-templates#44)
Co-authored-by: Leah <[email protected]>
1 parent 58acac4 commit d6a6dea

File tree

3 files changed

+44
-71
lines changed

3 files changed

+44
-71
lines changed

template/src/components/app.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
1-
import { h, Component } from 'preact';
1+
import { h } from 'preact';
22
import { Router } from 'preact-router';
33

44
import Header from './header';
55

6-
// Code-splitting is automated for routes
6+
// Code-splitting is automated for `routes` directory
77
import Home from '../routes/home';
88
import Profile from '../routes/profile';
99

10-
export default class App extends Component {
11-
12-
/** Gets fired when the route changes.
13-
* @param {Object} event "change" event from [preact-router](http://git.io/preact-router)
14-
* @param {string} event.url The newly routed URL
15-
*/
16-
handleRoute = e => {
17-
this.currentUrl = e.url;
18-
};
10+
const App = () => (
11+
<div id="app">
12+
<Header />
13+
<Router>
14+
<Home path="/" />
15+
<Profile path="/profile/" user="me" />
16+
<Profile path="/profile/:user" />
17+
</Router>
18+
</div>
19+
)
1920

20-
render() {
21-
return (
22-
<div id="app">
23-
<Header />
24-
<Router onChange={this.handleRoute}>
25-
<Home path="/" />
26-
<Profile path="/profile/" user="me" />
27-
<Profile path="/profile/:user" />
28-
</Router>
29-
</div>
30-
);
31-
}
32-
}
21+
export default App;

template/src/routes/home/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h } from 'preact';
2-
import style from './style';
2+
import style from './style.css';
33

44
const Home = () => (
55
<div class={style.home}>
Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
1-
import { h, Component } from 'preact';
2-
import style from './style';
3-
4-
export default class Profile extends Component {
5-
state = {
6-
time: Date.now(),
7-
count: 10
8-
};
9-
10-
// update the current time
11-
updateTime = () => {
12-
this.setState({ time: Date.now() });
13-
};
14-
15-
increment = () => {
16-
this.setState({ count: this.state.count+1 });
17-
};
18-
19-
// gets called when this route is navigated to
20-
componentDidMount() {
21-
// start a timer for the clock:
22-
this.timer = setInterval(this.updateTime, 1000);
23-
}
24-
25-
// gets called just before navigating away from the route
26-
componentWillUnmount() {
27-
clearInterval(this.timer);
28-
}
29-
30-
// Note: `user` comes from the URL, courtesy of our router
31-
render({ user }, { time, count }) {
32-
return (
33-
<div class={style.profile}>
34-
<h1>Profile: {user}</h1>
35-
<p>This is the user profile for a user named { user }.</p>
36-
37-
<div>Current time: {new Date(time).toLocaleString()}</div>
38-
39-
<p>
40-
<button onClick={this.increment}>Click Me</button>
41-
{' '}
42-
Clicked {count} times.
43-
</p>
44-
</div>
45-
);
46-
}
1+
import { h } from 'preact';
2+
import {useEffect, useState} from "preact/hooks";
3+
import style from './style.css';
4+
5+
// Note: `user` comes from the URL, courtesy of our router
6+
const Profile = ({ user }) => {
7+
const [time, setTime] = useState(Date.now());
8+
const [count, setCount] = useState(10);
9+
10+
useEffect(() => {
11+
let timer = setInterval(() => setTime(Date.now()), 1000);
12+
return () => clearInterval(timer);
13+
}, []);
14+
15+
return (
16+
<div class={style.profile}>
17+
<h1>Profile: {user}</h1>
18+
<p>This is the user profile for a user named { user }.</p>
19+
20+
<div>Current time: {new Date(time).toLocaleString()}</div>
21+
22+
<p>
23+
<button onClick={() => setCount((count) => count + 1)}>Click Me</button>
24+
{' '}
25+
Clicked {count} times.
26+
</p>
27+
</div>
28+
);
4729
}
30+
31+
export default Profile;

0 commit comments

Comments
 (0)