0
import React from 'react';

class App extends React.Component { 

  getTime() {
    return new Date().toLocaleDateString();
  }
  state = { time: null };

  render() {
    return (
    <div htmlFor="search">
      <label className="search" htmlFor="username">Name Please: </label>
      <input id="username" type="text" />
      <button style={{backgroundColor: 'blue', color: 'white'}}> Submit </button>
      <p>{getTime()}</p>
    </div>
  )
}
}

export default App;

I've been trying to get the time and display it in React but I keep getting errors. Line 16:11: 'getTime' is not defined no-undef But I've been doing just as tutorial told me to. What am I doing wrong?

1
  • 4
    this.getTime()? Commented Apr 12, 2021 at 9:40

2 Answers 2

2
  • first of all usually we define state in the beginning so just put it before getTime function

  • second thing - to be able to use functions in class components in react you have to add this before so just make it

    <p>{this.getTime()}</p>

Sign up to request clarification or add additional context in comments.

Comments

1

You should use this keyword for referring to the class and then methods and properties. So this.getTime(); should be used.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.