Skip to content

Instantly share code, notes, and snippets.

@oshell
Created May 21, 2019 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oshell/bb1b3eec49a98cf6d59cef44806f0fa6 to your computer and use it in GitHub Desktop.
Save oshell/bb1b3eec49a98cf6d59cef44806f0fa6 to your computer and use it in GitHub Desktop.
react example reset state when going back
<html>
<head>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
class Foo extends React.Component {
state = {
loading: false
}
componentDidMount() {
console.log('mounted');
}
leave() {
this.setState({
loading: true
});
setTimeout(() => {
window.location.href = 'test.html';
}, 2000);
}
render() {
return this.state.loading ? <div>loading...</div> : <button onClick={this.leave.bind(this)}>leave</button>;
}
}
ReactDOM.render(
<Foo />,
document.getElementById('app')
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment