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
| const reverseString = input => { | |
| const stack = [] | |
| for (let s of input) { | |
| stack.push(s) | |
| } | |
| let output = "" | |
| while (stack.length > 0) { | |
| output += stack.pop() | |
| } |
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
| style = loading => { | |
| return { | |
| transition: '0.5s filter linear', | |
| filter: `${loading ? 'blur(50px)' : ''}`, | |
| } | |
| } | |
| render() { | |
| const { currentImage, loading } = this.state | |
| const { alt } = this.props |
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
| fetchImage = src => { | |
| const image = new Image() | |
| image.onload = () => this.setState({ currentImage: this.loadingImage.src, loading: false }) | |
| image.src = src | |
| this.loadingImage = image | |
| } |
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
| componentDidMount() { | |
| this.fetchImage(this.props.image) | |
| } |
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
| class ProgressiveImage extends Component { | |
| state = { | |
| currentImage: this.props.preview, | |
| loading: true, | |
| } |
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
| <ProgressiveImage | |
| preview="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAS......" | |
| image={`/static/images/burgers/${id}.jpg`} | |
| /> |
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
| // Render function of ProgressiveImage | |
| render() { | |
| return this.props.render(loading) | |
| } | |
| // Usage | |
| render() { | |
| return ( | |
| <ProgressiveImage | |
| render={(src, loading) => ( |
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
| class ProgressiveImage extends Component { | |
| state = { | |
| currentImage: this.props.preview, | |
| loading: true, | |
| } | |
| componentDidMount() { | |
| this.fetchImage(this.props.image) | |
| } |
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
| var cars = this.cars.map(car => ... | |
| var message = 'No cars' | |
| if (cars.length) { | |
| var randomCar = null | |
| message = `The best car is ${randomCar}` | |
| ... | |
| // becomes | |
| const cars = this.cars.map(car => ... |
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
| onClick={function(e) { | |
| this.setSelectedCar(e.target.value) | |
| }.bind(this)} | |
| // becomes | |
| onClick={e => this.setSelectedCar(e.target.value)} |