나는 react.js의 초보자입니다. 간단한 fizzbuzz 예제를 구현하려고합니다.최대 호출 스택 크기가 내 reactjs 코드를 초과했습니다.
class App extends React.Component {
constructor(props){
super(props);
this.state = {
value : '',
number: 0
};
this.handleIncrement = this.handleIncrement.bind(this);
this.handleDecrement = this.handleDecrement.bind(this);
this.fizzBuzz = this.fizzBuzz.bind(this);
}
fizzBuzz(nb){
if(nb % 15 == 0 && nb!= 0)
this.setState({value : `FizzBuzz `});
else if(nb % 5 == 0 && nb!= 0)
this.setState({value : `Buzz `});
else if(nb % 3 == 0 && nb!= 0)
this.setState({value : `Fizz `});
else
this.setState({value : ''});
}
handleIncrement() {
let nb = this.state.number + 1;
this.setState({number: nb});
this.fizzBuzz(nb);
}
handleDecrement() {
let nb = this.state.number-1;
if(nb >= 0)
{ this.setState({number: nb});
this.fizzBuzz(nb);
}
else
{
this.setState({number: 0});
}
}
render() {
return (
<div>
<h1> {this.state.number} </h1>
<h1> {this.state.value} </h1>
<input type="button" value="add" onClick={this.handleIncrement()} />
<input type="button" value="sub" onClick={this.handleDecrement()} />
</div>
);
}
}
ReactDOM.render(<App/>,document.getElementById('app'));
: 여기
maximum call stack size exceeded
내 코드입니다 : 전반적으로, 나는 반응과 그 구성 요소가 작동하지만 내가 codepen에이 코드를 실행하려고 할 때, 나는이 오류를 얻을 방법의 요점을 가지고 생각
누군가 내가 실수 한 것을 지적 할 수 있습니까? 지난 2 시간 동안 나를 혼란스럽게 만들었다.
안부
'this.handleIncrement()'는 this.handleIncrement'가되어야합니다. 왜냐하면 당신은이 때문에 컴포넌트를 꾸준히 다시 생각하고 있다고 생각합니다. –
'this.handleIncrement()'와'this.handleDecrement()'를 호출하지 마십시오. 'render' 메소드에서. –
this.handleIncrement() 및 this.handleDecrement()를 호출하지 않는다는 의미가 무엇인지 이해하지 못하겠습니까 ?? 정교하게 제발 주시겠습니까? –