-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathejemploCicloDeActualizacion.js
101 lines (89 loc) · 2.27 KB
/
ejemploCicloDeActualizacion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React, { Component } from 'react'
import PropTypes from 'prop-types'
const ANIMAL_IMAGES = {
panda: 'https://goo.gl/oNbtoq',
cat: 'https://goo.gl/PoQQXb',
dolphin: 'https://goo.gl/BbiKCd'
}
const ANIMALS = Object.keys(ANIMAL_IMAGES)
class AnimalImage extends Component {
state = { src: ANIMAL_IMAGES[this.props.animal] }
componentWillReceiveProps (nextProps) {
console.clear()
console.log('1. componentWillReceiveProps', nextProps)
this.setState({ src: ANIMAL_IMAGES[nextProps.animal] })
}
shouldComponentUpdate (nextProps) {
console.log('2. shouldComponentUpdate', nextProps)
return this.props.animal !== nextProps.animal
}
componentWillUpdate (nextProps, nextState) {
console.log('3. componentWillUpdate', nextProps, nextState)
const img = document.querySelector('img')
console.log('from img element', { alt: img.alt })
// web animations api
img.animate([ {
filter: 'blur(0px)'
}, {
filter: 'blur(2px)'
}], {
duration: 500,
easing: 'ease'
})
}
componentDidUpdate (prevProps, prevState) {
console.log('4. componentDidUpdate')
const img = document.querySelector('img')
img.animate([
{
filter: 'blur(2px)'
},
{
filter: 'blur(0px)'
}
], {
duration: 1500,
easing: 'ease'
})
console.log('from img element', { alt: img.alt })
}
render () {
console.log('-> render')
return (
<div>
<p>Selected {this.props.animal}</p>
<img
alt={this.props.animal}
src={this.state.src}
width='250'
/>
</div>
)
}
}
AnimalImage.propTypes = {
animal: PropTypes.oneOf(ANIMALS)
}
class EjemploDeCicloDeActualizacion extends Component {
state = { animal: 'panda' }
_renderAnimalButton = (animal) => {
return (
<button
// disabled={animal === this.state.animal}
key={animal}
onClick={() => this.setState({ animal })}>
{animal}
</button>
)
}
render () {
return (
<div>
<h4>Ciclo de Actualización, Ejemplo de: ComponentDidUpdate</h4>
{ANIMALS.map(this._renderAnimalButton)}
<AnimalImage animal={this.state.animal} />
</div>
)
}
}
export default EjemploDeCicloDeActualizacion