-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateRef.jsx
52 lines (47 loc) · 1.23 KB
/
CreateRef.jsx
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
import React from "react";
const Emoji = ({ emoji }) => {
return (
<span role="img" aria-label="elefant">
{emoji}
</span>
);
};
class CreateRef extends React.Component {
/* Crea una referencia(crea un objeto) para guardar el elemento textInput del DOM */
inputRef = React.createRef();
handleFocus = () => {
if (this.inputRef) {
console.log("Focus");
/* Estamos accediendo la propiedad "current" para obtener el nodo del DOM */
this.inputRef.current.focus();
}
};
handleBlur = () => {
if (this.inputRef) {
console.log("Blur");
this.inputRef.current.blur();
}
};
handleValue = () => {
if (this.inputRef) {
console.log("Value");
console.log(this.inputRef.current.value);
console.log(this.inputRef);
}
};
render() {
return (
<>
<h2>
Create Ref <Emoji emoji="🐘" />
{/* Agregando una referencia a un elemento del DOM */}
<input type="text" ref={this.inputRef} />
<button onClick={this.handleFocus}>Focus</button>
<button onClick={this.handleBlur}>Blur</button>
<button onClick={this.handleValue}>Value</button>
</h2>
</>
);
}
}
export default CreateRef;