I feel like I am missing something probably pretty silly. I have a modal form pop up if a user wants to change their avatar photo. And the the function is running and updating my database, it just gets messed up when I'm changing the state ... so here is the function to handle the modal submit:
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);
})
}
And here is the modal:
<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>
I have tried binding this function inline, and in constructor as well ... but it just tells me another error that it's already bound to component.
Again, the function is working, it just gets hung up on the this.setState after the response from the server.
Any help appreciated, thanks!