gpt4 book ai didi

reactjs - 输入变化重置计时器

转载 作者:行者123 更新时间:2023-12-03 14:18:56 24 4
gpt4 key购买 nike

大家好,我最近开始学习 React,但遇到了一些问题。我正在尝试制作简单的 react 应用程序,我正在制作的组件之一是秒表。
我遇到的问题是,当我开始输入秒表输入时,计时器会重置。

这是我的主要组成部分:

import React, { Component } from 'react';
import Clock from '../components/Clock.jsx';
import Stopwatch from '../components/Stopwatch.jsx';
import '../css/App.css';
import { Form, FormControl, Button } from 'react-bootstrap';

class App extends Component {
constructor(props) {
super(props);
this.state = {
deadline: 'December 25, 2018',
newDeadline: '',
timer: 60,
newTimer: '',
};
}

changeDeadline() {
this.setState({
deadline: this.state.newDeadline,
});
}

changeTimer() {
this.setState({
timer: this.state.newTimer,
});
}

render() {
return (
<div className='app'>
<div className='app_title'>
Countdown to {this.state.deadline}
</div>
<Clock
deadline = {this.state.deadline}
/>
<Form inline >
<FormControl
className="deadline_input"
type="text"
placeholder="New date"
onChange={event => this.setState({newDeadline: event.target.value})}
onKeyPress={event => {
if (event.key === 'Enter') {
event.preventDefault();
this.changeDeadline();
}
}}
/>
<Button onClick={() => this.changeDeadline()} >
Submit
</Button>
</Form>

<div className="stopwatch_title">
Stopwatch from {this.state.timer} seconds
</div>

<Form inline>
<FormControl
className="stopwatch_input"
type="text"
placeholder="Enter time"
onChange={event => this.setState({newTimer: event.target.value})}
onKeyPress={event => {
if (event.key === 'Enter') {
event.preventDefault();
this.changeTimer();
}
}}
/>
<Button onClick={() => this.changeTimer()} >
Submit
</Button>
</Form>

<Stopwatch
timer = {this.state.timer}
/>

</div>
);
}
}

export default App;

和我的秒表组件:

import React, {Component} from  'react';
import '../css/App.css';
import { Button } from 'react-bootstrap';


class Stopwatch extends Component {
constructor(props) {
super(props);
this.state = {
stopwatch: props.timer,
};
this.decrementer = null;
}

componentWillReceiveProps(nextProps) {
clearInterval(this.decrementer);
this.timerCountdown(nextProps.timer);
}

timerCountdown(newTimer) {

// First we update our stopwatch with new timer
this.setState({
stopwatch: newTimer
});

}

startTimer() {
// Then we decrement stopwatch by 1 every second
this.decrementer = setInterval( () => {
this.setState({
stopwatch: this.state.stopwatch -1,
});
},1000);
}

componentDidUpdate() {
if (this.state.stopwatch < 1) {
clearInterval(this.decrementer);
alert('Countdown finished');
}
}

render() {
return(
<div>
<Button onClick={() => this.startTimer()} >
Start
</Button>
<div className="stopwatch"> {this.state.stopwatch} </div>
</div>
);
}
}

export default Stopwatch;

这是问题的 gif /image/Hz1kN.jpg
正如你所看到的,我的计时器在我开始输入输入后重置。我希望仅当用户按下 enter 键或使用 submit 按钮时才重置。

我尝试过做这样的事情:

  <input value={this.state.newTimer} onChange={evt => this.updateInputValue(evt)}/>

updateInputValue: function(evt) {
this.setState({
newTimer: evt.target.value
});
}

但这对我不起作用。您可以在此处查看正在运行的代码:https://karadjordje.github.io/countdown-stopwatch-react/

最佳答案

您将在组件收到的每个新 prop 上停止 interval
您可以处理本地状态中的时间,也可以显式地从父级传递正确的新值。

我做了一个小的基本示例,以便您可以看到数据流以及每个事件如何负责一小部分数据。
希望有帮助。

class App extends React.Component {

state = {
startTime: 5,
currentTime: 5,
textInput: ''
}

startTimer = () => {
if (this.interval) {
clearInterval(this.interval);
}
this.interval = setInterval(() => {
this.setState(prev => {
if (prev.currentTime === 0) {
this.stopTimer();
return { ...prev, currentTime: prev.startTime };
} else {
return {
...prev,
currentTime: prev.currentTime - 1
}
}
})
}, 1000)
}

stopTimer = () => {
clearInterval(this.interval);
}

updateInput = ({ target }) => {
this.setState(prev => ({ textInput: target.value }));
}

setStartTime = () => {
this.stopTimer();
this.setState(({ textInput }) => ({ startTime: textInput, currentTime: textInput, textInput: '' }));
}

render() {
const { currentTime, textInput } = this.state;
return (
<div >
<div>{currentTime}</div>
<button onClick={this.startTimer}>Start timer</button>
<div>
<input placeholder="Enter start time" value={textInput} onChange={this.updateInput} />
<button onClick={this.setStartTime}>Set Start time</button>
</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

关于reactjs - 输入变化重置计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49480037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com