I'm using history.push('/, { justAddedItem: true })
to redirect to a route and let the target component know a form has been successfully submitted. It works as expected: I'm able to access this.props.history.state.justAddedItem
on the Component that the /
route renders.
However, when I refresh the page this.props.history.state.justAddedItem
is still there! and of course it shouldn't.
In my case, I'm using this to render a success toast (I'm using react-toastify
), needless to say, I don't want to show this toast on every page refresh.
If I'm in /
THEN go to another route, then go back to /
and refresh THEN history.state
will be undefined
Component 1
componentWillReceiveProps(nextProps) {
// addedItem comes from the redux store
if (isEmpty(this.props.addedItem) && !isEmpty(nextProps.addedItem)) {
this.props.clearAddedItem()
this.props.history.push('/', { justAddedItem: true })
}
}
Component 2
componentDidMount() {
this.props.getItems()
if (this.props.location.state && this.props.location.state.justAddedItem) {
this.setState({ showAddedToast: true })
}
}
componentDidUpdate(prevProps, prevState) {
if (prevState.showAddedToast) {
toast.success('Item added successfully!', {
position: toast.POSITION.TOP_CENTER,
})
}
}