2

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

1 Answer 1

1

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>
           );
      }
Sign up to request clarification or add additional context in comments.

7 Comments

I get: error Parsing error: Unexpected token 3 | export default class App extends React.Component {
Updated my answer. Can u check now should be fine this time
Now I get that tick is not defined
Sorry my bad it should be this.tick() in setInterval. Check my updated answer
Ok, now I'm in the start situation, no errors but it prints only 2.. Thanks for your patience
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.