폼에서 상태를 업데이트하는 반응 구성 요소가 있다고 가정 해 보겠습니다.React에 상태를 저장하는 것이 언제 안전합니까?
class Form extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.state = {
someCheckboxState: false,
}
}
render() {
return (
<input onChange={this.handleChange} checked={this.state.someCheckboxState} />
);
}
handleChange(event) {
this.setState({
someCheckboxState: event.target.checked,
});
}
}
이제 상태를 서버 (또는 어딘가에)로 보내려고합니다. 난 그냥이
handleChange(event) {
this.setState({
someCheckboxState: event.target.checked,
});
SendStateToServer(JSON.stringify(this.state)); // BAD! Not yet mutated
}
를 할 경우 나는 render
에 넣을 수 있지만, 초기뿐만 아니라 렌더링에 다음이뿐만 아니라 서버로 전송 얻을 것이다 상태를 render
라는 함수를 보낼 바보 같다.
언제 상태를 유지/직렬화해도됩니까?
두번째 파라미터 실행할 콜백이고 – MichaelB