1

I am new in reactjs, practicing some codes but getting some error on onclick function(Error is function is not executing), can anyone help to find out the error I have done, here is my code

import React, { Component } from "react";

class Hello extends Component {
  constructor(props) {
    super(props);

    this.state = {
      time: Date.now()
    };

    this.changeTime = this.changeTime.bind(this);
  }

  changeTime() {

    alert('hi');
    this.setState({
      time: Date.now()
    });
  }

  render() {
    return (
      <div>
        <center>
          <h3>Current Time : {this.state.time}</h3>
          <br />
          <button onlcick={this.changeTime}>Change Time </button>
        </center>
      </div>
    );
  }
}

export default Hello;

1 Answer 1

1

it should be <button onClick={this.changeTime}>Change Time </button>

not <button onlcick={this.changeTime}>Change Time </button>

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

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.