Created
May 21, 2019 13:44
-
-
Save oshell/bb1b3eec49a98cf6d59cef44806f0fa6 to your computer and use it in GitHub Desktop.
react example reset state when going back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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