简体   繁体   English

我应该解决一个计时器的 React 练习并通过按钮重置它。我在重置我的计时器时遇到问题

[英]I should solve a React exercise of a timer and reset it by a button.I have problem with resetting my timer

I want to create a timer with a reset button.我想创建一个带有重置按钮的计时器。 I must do this exercise in an initial code that is written by React.我必须用 React 编写的初始代码来做这个练习。 This is the initial code.这是初始代码。

 import React, {Component} from 'react'; import './Timer.css'; export class Timer extends Component { render() { return <div className="container"> <div className="timer"> </div> <button>Reset Timer</button> </div>; } }

I've tried two solution but each of them returned different error.我尝试了两种解决方案,但每个解决方案都返回了不同的错误。 Here is my first solution.(figure 1) figure 1这是我的第一个解决方案。(图 1)图 1

and the error is (figure 2) figure 2错误是(图2)图2

and my second solution is this ... (figure 3) figure 3我的第二个解决方案是...(图3)图3

 import React, {Component} from 'react'; import './Timer.css'; const initialState = { seconds: 0 } export class Timer extends Component { constructor(props) { super(props); this.state = initialState; } tick() { this.setState(state => ({ seconds: state.seconds + 1 })); } componentDidMount() { this.interval = setInterval(() => this.tick(), 1000); } componentWillUnmount() { clearInterval(this.interval); } resetTimer() { this.setState(initialState); } render() { return <div className="container"> <div className="timer"> {this.state.seconds} </div> <button onClick = {this.resetTimer}>Reset Timer</button> </div>; } }

and its error is that ... (figure 4) figure 4它的错误是......(图4)图4

How can I solve the problem of resetting the timer?如何解决重置定时器的问题?

The problem comes from the "this" scope.问题来自“this”范围。

Either you have to bind the function you're using in the class.要么你必须绑定你在类中使用的函数。

constructor( props ){
   super( props );
   this.resetTimer = this.resetTimer.bind(this);
}

A second option is to use arrow functions when you declare your functions in order to maintain the scope of "this" on the class.第二种选择是在声明函数时使用箭头函数,以维护类上“this”的范围。

resetTimer = () => {
   this.setState(initialState);
}

instead of writing而不是写作

`resetTimer() {
    this.setState(initialState);
    }`

use arrow function使用箭头函数

const resetTimer=()=> { this.setState(initialState); }

this will work这会起作用

second Solution - this is because you have not bind the function and calling it in the click event Please refer Handling events第二种解决方案- 这是因为您没有绑定函数并在点击事件中调用它请参阅处理事件

So add this line inside the constructor所以在构造函数中添加这一行

this.resetTimer = this.resetTimer.bind();

I hope this solves your problem :)我希望这能解决你的问题:)

You have to bind the method call with the event as suggested by other users If you don't bind the method, It will be always be called with the re-render您必须按照其他用户的建议将方法调用与事件绑定如果您不绑定该方法,它将始终在重新渲染时被调用

First approach第一种方法

Inside constructor this.methodName = this.bind.methodName(this);内部构造函数this.methodName = this.bind.methodName(this);

Inside render()内部渲染()

render(){
return(
<button onClick={this.methodName}></button>
)
}

Second approach第二种方法

render(){
 return(
  <button onClick={()=>this.methodName()}></button>
 )
}

Third approach第三种方法

methodName = () => {
//scope
}

render(){
 return(
  <button onClick={this.methodName}></button>
 )
}

The third type is not available for older versions of react you have to use class experimental plugin for that, In that case you should always you the first approach第三种类型不适用于旧版本的反应,您必须为此使用类实验插件,在这种情况下,您应该始终使用第一种方法

The second approach should always be used when you need to pass a parameter otherwise don't use that当您需要传递参数时,应始终使用第二种方法,否则不要使用该方法

Also please note If you just write另外请注意如果你只是写

<button onClick={this.methodName()}></button>

That means you are calling the method, but without binding it with event ie whether you click or not the method will always be called这意味着您正在调用该方法,但没有将其与事件绑定,即无论您是否单击该方法都将始终被调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM