this.setState
이 상태를 즉시 변경하는지 궁금합니다. 그렇게하지 않는 것 같습니다. OK입니다 -ReactJS : this.setState가 즉시 적용됩니까?
, 설명의이 체크 박스가 있다고 가정하자하려면 ....이 예에서
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
switch: false
};
this.switch = this.switch.bind(this);
}
clickCoverSwitch(){
console.log("Before clicking - ", this.state.switch);
this.setState({switch: !this.state.switch});
console.log("Now, the state is - ", this.state.switch);
}
render() {
return (
<input onClick={this.clickCoverSwitch} defaultChecked={this.state.coverSwitch} type="checkbox">
</input>
);
}
이 체크 박스는 기본적으로 꺼져 있습니다.
그러나 스위치 (즉, 체크 박스)를 클릭하면 콘솔에 다음 메시지가 표시됩니다.
Now, the state is - false
두 번째 줄
Before clicking - false
this.state.switch
가
this.setState({switch: !this.state.switch})
를 통해 변경해야하기 때문에,
true
보여줄 예정이다.
나는 이것을 어떻게 해석해야하는지 잘 모르겠습니다. 조언을 주시면 감사하겠습니다.
아니요, 비동기입니다. 설정 한 후에 상태를 기록하려면'setState'의 콜백을 사용할 수 있습니다 : http://stackoverflow.com/questions/40641998/when-is-it-safe-to-save-state-in-react/ 40642027 # 40642027 –
부분적으로 참입니다. 'setState'는 동기식 일 수도 있습니다. – Andreyco