I write a JSX code in order to create a reset button in which i can made a reset to the timer value using "state".The problem is when i click to the button nothing happends instead of getting the value changed.(Even when i want to display a console msg through the reset function nothing happends)
A created a "state.timer" variable in order to add the value of to display. A function to made the reset to the "timer" value and change it to 0. A button which call the reset function in order to made the reset.
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = {
timer: 90
};
function resetTime() {
this.setState({ timer: 0 });
}
}
render() {
return (
<div className="App">
<h2>{this.state.timer}</h2>
<button onClick={this.resetTime}>Reset</button>
</div>
);
}
}
ReactDOM.render(<Timer />, document.getElementById("root"));
Expected to display 0 when i click to the reset button instead the value still display 90.
this.setState({timer:0})...