I can't figure out what's wrong with my code.
import React from 'react';
import './App.css';
class Clock extends React.Component
{
constructor(props)
{
super(props);
//defining state
this.state={date:new Date()};
}
updateTime()
{
//updating the time by set state
this.setState( (state,props) => {date:new Date()} );
}
render()
{
return (
<div>
<h2>Now the time is {this.state.date.toLocaleTimeString()}</h2>
<button onClick={this.updateTime}>Update Time</button>
</div>
);
}
}
export default Clock;
Getting below error on updating the state i.e. when the button is clicked.
TypeError: Cannot read property 'setState' of undefined updateTime
C:/Users/YV/YVSCodes/React-Van/hi-app/src/setState-event-binding-exampleComponent.js:21
18 | updateTime() 19 | { 20 | //updating the time by set state21 | this.setState( (state,props) => {date:new Date()} );