0
我有一个非常混乱的任务,我需要你的帮助。 我需要为每个任务创建带定时器的todos。所以用户可以创建他自己的任务 - >启动计时器 - >停止计时器 - >关闭任务。每个任务都有按钮启动和停止。React的残疾人按钮
问题是只有一个定时器可以同时工作。因此,即使一个定时器工作,也应禁用另一个按钮“开始”。 我想这个决定靠近handleStartClick(改变数值按钮?)。
这是代码的主要部分:
export default class TodoItem extends Component {
constructor(props) {
super(props);
this.state = { secondsStart: this.props.minSeconds }
this.handleStartClick = this.handleStartClick.bind(this)
}
static propTypes = {
todo: PropTypes.object.isRequired,
deleteTodo: PropTypes.func.isRequired,
completeTodo: PropTypes.func.isRequired,
}
static defaultProps = {
minSeconds: 0
}
getSeconds =() => {
return ('0' + this.state.secondsStart % 60).slice(-2)
}
getMinutes =() => {
return Math.floor('0' + this.state.secondsStart/60)
}
getHoures =() => {
return Math.floor(this.state.secondsStart/60)
}
handleSave = (id, text) => {
if (text.length === 0) {
this.props.deleteTodo(id)
}
}
handleStartClick =() => {
this.incrementer = setInterval(() => {
this.setState({secondsStart:(this.state.secondsStart + 1)
});
}, 1000)
}
handleStopClick =() => {
clearInterval(this.incrementer)
}
render() {
const { todo, completeTodo, deleteTodo} = this.props
let element
if (this.state.todo) {
element = (
<TodoTextInput text={todo.text}
onSave={(text) => this.handleSave(todo.id, text)} />
)
} else {
element = (
<div className="view">
<input className="toggle"
type="checkbox"
checked={todo.completed}
onChange={() => completeTodo(todo.id)} />
<label>
{todo.text}
</label>
<div className="buttons">
<h6>{this.getHoures()}:{this.getMinutes()}:{this.getSeconds()}</h6>
{(this.state.secondsStart === 0)
? <button className="timer-start" onClick={this.handleStartClick}>Start</button>
: <button className="timer-stop" onClick={this.handleStopClick}>Stop</button>
}
</div>
<button className="destroy"
onClick={() => deleteTodo(todo.id)} />
</div>
)
}
对不起,也许实在是太大了,但我真的不知道我可以隐藏,或许真的是有用的。 我将非常感谢您的帮助。
正如我右理解,这些状态我可以控制这样的: handleStartClick =()=> { this.incrementer =的setInterval(()=> { this.setState({secondsStart:(this.state。 secondsStart + 1) }); },1000) this.setState({timerRunning:true }); } –
是的。检查反应setState方法的确切细节。 –
谢谢,这对我真的很有帮助。 –