Parámetros en funciones JSX
Supongamos que nuestra función showMess recibe como parámetro
el nombre de la persona a la que queremos saludar:
function showMess(name) {
alert('hello, ' + name);
}
Se puede pasar este parámetro al vincular la función al evento. Para ello, la llamada a nuestra función debe envolverse en una función flecha:
function App() {
function showMess(name) {
alert('hello, ' + name);
}
return <div>
<button onClick={() => showMess('user')}>show</button>
</div>;
}
De esta manera podemos vincular la misma función a varios botones con diferentes parámetros:
function App() {
function showMess(text) {
alert(text);
}
return <div>
<button onClick={() => showMess('user1')}>show1</button>
<button onClick={() => showMess('user2')}>show2</button>
</div>;
}
Se dan tres botones:
function App() {
return <div>
<button>act1</button>
<button>act2</button>
<button>act3</button>
</div>;
}
Haz que al hacer clic en el primer botón
se muestre el número 1 mediante alert, al hacer clic
en el segundo botón - el número 2, y al hacer clic
en el tercero - el número 3.