Skip to content

Instantly share code, notes, and snippets.

View perjansson's full-sized avatar

Per Jansson perjansson

View GitHub Profile
@perjansson
perjansson / reverse_string_using_stack.js
Created January 2, 2018 19:40
reverse a string using a stack
const reverseString = input => {
const stack = []
for (let s of input) {
stack.push(s)
}
let output = ""
while (stack.length > 0) {
output += stack.pop()
}
style = loading => {
return {
transition: '0.5s filter linear',
filter: `${loading ? 'blur(50px)' : ''}`,
}
}
render() {
const { currentImage, loading } = this.state
const { alt } = this.props
fetchImage = src => {
const image = new Image()
image.onload = () => this.setState({ currentImage: this.loadingImage.src, loading: false })
image.src = src
this.loadingImage = image
}
componentDidMount() {
this.fetchImage(this.props.image)
}
class ProgressiveImage extends Component {
state = {
currentImage: this.props.preview,
loading: true,
}
<ProgressiveImage
preview="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAS......"
image={`/static/images/burgers/${id}.jpg`}
/>
// Render function of ProgressiveImage
render() {
return this.props.render(loading)
}
// Usage
render() {
return (
<ProgressiveImage
render={(src, loading) => (
class ProgressiveImage extends Component {
state = {
currentImage: this.props.preview,
loading: true,
}
componentDidMount() {
this.fetchImage(this.props.image)
}
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 => ...
onClick={function(e) {
this.setSelectedCar(e.target.value)
}.bind(this)}
// becomes
onClick={e => this.setSelectedCar(e.target.value)}