I am using react-router browserHistory
to navigate to path.
browserHistory.push({
pathname: '/mycomponent',
state: { someValue: 'value'},
});
So this will navigate to mycomponent. As soon as i reach to mycomponent i want to clear key someValue
. so that when i refresh the page it won't contain that value.
export class myComponent extends component {
componentWillMount() {
const value = this.props.location.state.someValue;
// clear state.someValue from history
}
}
Any help would be appreciated.
You can simply use history.replace()
to clear the state, this way:
Just
import { useHistory } from 'react-router-dom';
And then const history = useHistory();
Finally:
useEffect(() => {
history.replace();
}, []);
I think that you can use browserHistory replace
method to replace history state with new one without someValue
defined:
export class myComponent extends component {
componentWillMount() {
const value = this.props.location.state.someValue;
// clear state.someValue from history
browserHistory.replace({
pathname: '/mycomponent',
state: {}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With