-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultProps.js
More file actions
129 lines (93 loc) · 3.03 KB
/
DefaultProps.js
File metadata and controls
129 lines (93 loc) · 3.03 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// React también tiene una opción para establecer accesorios predeterminados.
// Por ejemplo, si declara
MyComponent.defaultProps = { location: 'San Francisco' }
// accesorio de ubicación que se establece en la cadena San Francisco
//
// Defina accesorios predeterminados en este componente que especifiquen un accesorio itemscon un valor de 0.
const ShoppingCart = (props) => {
return (
<div>
<h1>Shopping Cart Component</h1>
</div>
)
};
ShoppingCart.defaultProps = { items: 0 }
// anular props predeterminados
// La forma de anular los accesorios predeterminados es establecer explícitamente los valores de los accesorios para un componente.
const Items = (props) => {
return <h1>Current Quantity of Items in Cart: {props.quantity}</h1>
}
Items.defaultProps = {
quantity: 0
}
class ShoppingCart extends React.Component {
constructor(props) {
super(props);
}
render() {
{ /* change code below this line */ }
return <Items quantity={10}/>
}
};
// Output :
// Current Quantity of Items in Cart: 10
// usa PropTypes para definir los Props que esperas
// configurar propTypessu componente para que los datos sean de tipo array.
MyComponent.propTypes = { handleClick: PropTypes.func.isRequired }
// PropTypesse importa independientemente de React
import PropTypes from 'prop-types';
// propTypesel Itemscomponente que se necesita quantitycomo accesorio y verifique que sea de tipo number.
const Items = (props) => {
return <h1>Current Quantity of Items in Cart: {props.quantity}</h1>
};
// change code below this line
Items.propTypes = {
quantity: PropTypes.number.isRequired
};
Items.defaultProps = {
quantity: 0
};
class ShoppingCart extends React.Component {
constructor(props) {
super(props);
}
render() {
return <Items />
}
};
// Output:
// Current Quantity of Items in Cart: 0
// acceder a los accesorios con this.props
// El componente de clase ES6 utiliza una convención ligeramente diferente para acceder a los accesorios.
// Por ejemplo, si un componente de clase ES6 tiene un accesorio llamado data, usted escribe
{this.props.data} // en JSX.
// Example
class ReturnTempPassword extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
{ /* change code below this line */ }
<p>Your temporary password is: <strong>{this.props.tempPassword}</strong></p>
</div>
);
}
};
class ResetPassword extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h2>Reset Password</h2>
<h3>We've generated a new temporary password for you.</h3>
<h3>Please reset this password from your account settings ASAP.</h3>
{ /* change code below this line */ }
<ReturnTempPassword tempPassword="123456789" />
</div>
);
}
};