어제부터이 문제를 해결하고 도움이되지 않아서 혼란 스럽습니다. 문제는 다음과 같습니다.해야 할 일 목록이 있습니다. 모든 작업에는 타이머가 있습니다. 동시에 하나의 타이머 만 작동 할 수 있으므로 사용자는 동시에 하나의 작업 만 수행 할 수 있습니다. 타이머가 작동 중일 때 다른 작업에서 '시작'버튼을 사용할 수 없다고 생각했지만 모든 타이머가 여전히 함께 작동하기 때문에 setState에서 실수를했다고 가정합니다 = ( 설명서를 읽었지만 그렇지 않습니다. 또한 나에게 도움이. , 나는이 작업을 생성되는 파일 TodoTextInput을하고 난 여기 내 타이머를 붙여 넣기한다 어쩌면 생각하지만 이상하다. 모든 정보에 대한일회성 작업 타이머
내가 여기에 두 개의 파일을 둡니다. 이 주셔서 감사합니다 어떤 도움!
ToDoItem을(여기서 문제가)
import React, { Component, PropTypes } from 'react'
import classnames from 'classnames'
import TodoTextInput from './TodoTextInput'
export default class TodoItem extends Component {
constructor(props) {
super(props);
this.state = { secondsStart: this.props.minSeconds, timerRunning: false }
}
static propTypes = {
todo: PropTypes.object.isRequired,
deleteTodo: PropTypes.func.isRequired,
completeTodo: PropTypes.func.isRequired,
}
static defaultProps = {
minSeconds: 0
}
handleSave = (id, text) => {
if (text.length === 0) {
this.props.deleteTodo(id)
}
}
handleStartClick =() => {
if (!this.state.timerRunning) {
this.incrementer = setInterval(() => {
this.setState({
secondsStart: (this.state.secondsStart + 1)
});
}, 1000)
this.setState({
timerRunning: true,
currentTodoId: this.props.todo.id,
runningTodoId: this.props.todo.id
});
}
}
getSeconds =() => {
return ('0' + this.state.secondsStart % 60).slice(-2)
}
getMinutes =() => {
return Math.floor((this.state.secondsStart/60)%60)
}
getHoures =() => {
return Math.floor((this.state.secondsStart/3600)%24)
}
handleStopClick =() => {
clearInterval(this.incrementer)
this.setState({ timerRunning: false, currentTodoId: null, runningTodoId: null });
}
render() {
const { todo, completeTodo, deleteTodo} = this.props
const element = this.state.todo ? (
<TodoTextInput text={todo.text}
onSave={(text) => this.handleSave(todo.id, text)} />
) : (
<div className="view">
<input className="toggle"
type="checkbox"
checked={todo.completed}
onChange={() => completeTodo(todo.isRequired)} />
<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} disabled={this.state.timerRunning }>Start</button>
: <button className="timer-stop" onClick={this.handleStopClick} disabled={!this.state.timerRunning && this.state.runningTodoId !== this.state.currentTodoId}>Stop</button>
}
</div>
<button className="destroy"
onClick={() => deleteTodo(todo.id)} />
</div>
)
return (
<li className={classnames({
completed: todo.completed,
})}>
{element}
</li>
)
}
}
TodoTextInput(단지 경우)
import React, { Component, PropTypes } from 'react'
import classnames from 'classnames'
export default class TodoTextInput extends Component {
static propTypes = {
onSave: PropTypes.func.isRequired,
text: PropTypes.string,
placeholder: PropTypes.string,
newTodo: PropTypes.bool
}
state = {
text: this.props.text || ''
}
handleSubmit = e => {
const text = e.target.value.trim()
if (e.which === 13) {
this.props.onSave(text)
if (this.props.newTodo) {
this.setState({ text: '' })
}
}
}
handleChange = e => {
this.setState({ text: e.target.value })
}
handleBlur = e => {
if (!this.props.newTodo) {
this.props.onSave(e.target.value)
}
}
render() {
return (
<input className={
classnames({
'new-todo': this.props.newTodo
})}
type="text"
placeholder={this.props.placeholder}
autoFocus="true"
value={this.state.text}
onBlur={this.handleBlur}
onChange={this.handleChange}
onKeyDown={this.handleSubmit} />
)
}
}
나는이 문제가 시작할 때 모든 타이머를 사용하지 못하도록한다는 것을 이해합니다. 맞습니까? TodoItem 컴포넌트를 어디에서 사용하고 있습니까? –
예, 한 작업에서 '시작'을 클릭하면 다른 작업에서 '시작'을 클릭 할 수 없습니다. 이 작업에서 '중지'를 클릭 할 때만 다른 작업과 타이머를 실행할 수 있습니다. TodoItem 구성 요소 MainSection 구성 요소에서 가져옵니다. 앱의 가장 큰 부분입니다. –