Created
May 28, 2018 14:40
-
-
Save zxc0328/ae74aefb9fd027df3392d21b9df8b48c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import React, { Component } from "react"; | |
| import logo from "./logo.svg"; | |
| import "./App.css"; | |
| let count = 1; | |
| const fakeFetch = () => { | |
| return new Promise(resolve => { | |
| setTimeout(() => { | |
| count += 1; | |
| resolve(count); | |
| }, 1000); | |
| }); | |
| }; | |
| class App extends Component { | |
| state = { | |
| count: 1, | |
| flag: false | |
| }; | |
| componentDidMount() { | |
| this.startUploading(); | |
| } | |
| onClickHandler = () => { | |
| this.setState({ | |
| flag: true | |
| }); | |
| }; | |
| startUploading = async () => { | |
| for (let i = 0; i < 100; i++) { | |
| let count = await fakeFetch(); | |
| this.setState({ | |
| count | |
| }); | |
| if (this.state.flag) break; | |
| } | |
| }; | |
| render() { | |
| return ( | |
| <div className="App"> | |
| <header className="App-header"> | |
| <img src={logo} className="App-logo" alt="logo" /> | |
| <h1 className="App-title">{this.state.count}</h1> | |
| </header> | |
| <button onClick={this.onClickHandler}>Stop</button> | |
| <p className="App-intro"> | |
| To get started, edit <code>src/App.js</code> and save to reload. | |
| </p> | |
| </div> | |
| ); | |
| } | |
| } | |
| export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment