user10381316
user10381316

Reputation:

Basic example using React

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.

enter image description here


The error I get is:

enter image description here

Upvotes: 2

Views: 75

Answers (1)

Hemadri Dasari
Hemadri Dasari

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

Related Questions