After a an AJAX request has returned a response, I want to give a feedback message to the user for 5 seconds - e.g. "Saved!".
class FlashMessage extends Component {
state = { visible: false }
updateVisibility = () => {
this.setState({ visible: true }, () =>
this.setTimeout(this.setState({ visible: false }), 5000)
)
}
render() {
this.updateVisibility()
if (this.props.data && this.state.visible) {
return <div>Saved!</div>
}
return false
}
}
export default FlashMessage
Above is my futile attempt to create a component doing this, but obviously doesn't work because you:
Cannot update during an existing state transition (such as within
render
). Render methods should be a pure function of props and state."
So my question is - how can one show a message for n seconds following a AJAX response.
Unfortunately I cannot rely on onClick
here.