아마 내가 어리석은 뭔가를 놓치고있는 것처럼 느껴집니다. 사용자가 자신의 아바타 사진을 변경하려는 경우 모달 폼 팝업이 나타납니다. 그리고 함수가 나는 상태를 변경하고있을 때, 그것은 단지 엉망이됩니다 실행하고 내 데이터베이스를 업데이트 ... 그래서 여기 모달을 처리 할 수있는 기능입니다 제출 :반응 모달을 통해 양식을 제출할 때 undefined의 'setState'속성을 읽을 수 없습니다.
onModalSubmit: function() {
let data = new FormData();
data.append('id', this.state.user._id)
data.append('image', this.refs.image.files[0])
axios.patch('/api/userphoto', data, {
'Content-Type': 'multipart/form-data'
}).then(function (response) {
console.log(response);
window.localStorage.setItem('user', JSON.stringify(response.data.user));
this.setState({ user: response.data.user, modalIsOpen: false });
}).catch(function (error) {
console.log(error);
})
}
그리고 여기 모달입니다 :
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
style={customStyles}
contentLabel="Change Photo"
>
<h2>Change Photo</h2>
<div className="field">
<div className="ui center icon input">
<input type="file" name="image" ref="image"/>
<button className="ui button" onClick={this.closeModal}>Cancel</button>
<button className="ui button" onClick={this.onModalSubmit}>Submit</button>
</div>
</div>
</Modal>
나는이 함수를 인라인 바인딩 시도하고, 생성자에서뿐만 아니라 ...하지만 그냥 나에게 이미 구성 요소에 바인딩 된 또 다른 오류를 알 수있다.
다시 함수가 작동 중이며 서버에서 응답 한 후 this.setState에서 중단됩니다.
도움을 주시면 감사하겠습니다.
=이 사용할 self.setState (...) 대신? 패치 기능 내부에서 "this"키워드를 사용했을 수도 있습니다. – maembe
아, 미친 ... 나는 var self = this를 맨 위에 놓았고 문제가 해결되었습니다. 그것은 매우 혼란 스럽습니다. 그리고 저는 여전히 그것에 약간 혼란 스럽습니다. 나는 그것이 작동한다고 생각한다. –
감사합니다! @maembe –