I've been trying to learn react and next.
Using this clock example given here on codepen, found in the react docs here.
I tried implementing it on a local site.
Clock.js - in /components
export default class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
index.js - in /pages
import Clock from '../components/Clock'
export default () => (
<div>
<Header />
<div id="contentWrap">
<div><Clock /></div>
</div>
</div>
)
Running npm run dev then going to localhost:3000, it will initially load but it wont update each second like on codepen.
Have I missed something somewhere that allows it to update?
via Aden Laken
No comments:
Post a Comment