Reputation: 2329
I want to show error (or success) message using FlashMessage
while having my page reload at desired time
I'm using FlashMessage
and my code looks like
render() {
return (
{this.state.error ? <FlashMessage duration={5000}><strong>{this.state.error.message}</strong></FlashMessage> : ''}
//Some table here presenting data
<Button variant="outline-info" type="submit" size="lg" block className="button-custom" onClick={this.handleSubmit.bind(this)}>
Submit
</Button>
)}
for my stateful component, the error
is loaded by
handleSubmit(event) {
let data = {
name: this.state.name,
unit_price: this.state.unit_price,
length: this.state.length,
time: this.state.selectedDate,
instructor: this.state.instructor,
}
ApiService.postLesson(data)
.then(res => {
this.setState({
message: 'Success!'
});
})
.catch(error => {
this.setState({
message: error,
error: error,
});
console.log(error);
})
};
and my ApiService.postLesson
is
const instance = axios.create({
headers: {
"Content-Type": "application/json",
Authorization: 'Token ' + localStorage.getItem('token')
},
});
export default {
postLesson: data =>
instance({
'method': 'POST',
'url': BACKEND_LESSONS_URL,
'data': data
}),
// some other services
Now my problem is every time I click Submit, whether it's successful or not, it reloads the page. Because of that, I think my state is reloaded and the error is gone. If I add event.preventDefault()
in handleSubmit
, then I can see the message but then my content in the table would not be updated. What's a solution for this?
Upvotes: 7
Views: 2427
Reputation: 141
handleSubmit(event) {
// to prevent the reload!
event.preventDefault();
let data = {
name: this.state.name,
unit_price: this.state.unit_price,
length: this.state.length,
time: this.state.selectedDate,
instructor: this.state.instructor,
}
ApiService.postLesson(data)
.then(res => {
this.setState({
message: 'Success!'
});
// TODO: call the /lessons endpoint and set the state with the result!
/* WARNING: do not add the lesson manually to the state without fetching
* the lessons endpoint specifically if more than one person can do the
* update because you will see stale data!!!!
*/
})
.catch(error => {
this.setState({
message: error,
error: error,
});
console.log(error);
})
}
in the TODO comment, call your main endpoint (/lessons for example) and set the data in the state so the view will get updated without refreshing the whole page!
Upvotes: 0
Reputation: 196
Seems like you are on the right track and event.preventDefault()
is really necessary for what you are trying to achieve.
So, you can refresh the page on success by calling location.reload()
:
handleSubmit(event) {
event.preventDefault();
let data = {
name: this.state.name,
unit_price: this.state.unit_price,
length: this.state.length,
time: this.state.selectedDate,
instructor: this.state.instructor,
}
ApiService.postLesson(data)
.then(res => {
this.setState({
message: 'Success!'
});
location.reload(); // refreshes the page
})
.catch(error => {
this.setState({
message: error,
error: error,
});
console.log(error);
})
}
It's not clear why do you need to refresh the entire page and bootstrap react app after successful postLesson()
calls. However, there are potentially better options for your case:
handleSubmit(event) {
event.preventDefault();
let data = {
name: this.state.name,
unit_price: this.state.unit_price,
length: this.state.length,
time: this.state.selectedDate,
instructor: this.state.instructor,
}
ApiService.postLesson(data)
.then(res => {
this.setState({
message: 'Success!'
});
// 1. re-fetching list of items
// ApiService.getLessons().then((lessons) => {this.setState({lessons})})
// or 2. add newly posted lesson to the list
// this.setState({lessons: this.state.lessons.concat(data)})
})
.catch(error => {
this.setState({
message: error,
error: error,
});
console.log(error);
})
}
I hope this gives you a few ideas.
Upvotes: 4