Reputation:
I'm reading this tutorial about React.
I try to do this example. This is my code:
import React from 'react'
export default class App extends React.Component {
render() {
function tick() {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
}
return (
setInterval(tick, 1000)
);
}
}
It doesn't work, I get no errors, but it doesn't do what it should.
The error I get is:
Upvotes: 2
Views: 75
Reputation: 33974
You need to move the tick()
function out of the render method and the render method needs a return statement
tick = () => {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
return element;
}
render(){
return (
<div>{setInterval(this.tick(), 1000)}</div>
);
}
Upvotes: 1