Parameters in a JSX function
Let our function showMess
take as a parameter the name of the person we want to greet:
function showMess(name) {
alert('hello, ' + name);
}
We can pass this parameter when binding a function to an event. To do this, we should wrap our function call in an arrow function:
function App() {
function showMess(name) {
alert('hello, ' + name);
}
return <div>
<button onClick={() => showMess('user')}>show</button>
</div>;
}
This way we can bind the same function to several buttons with different parameters:
function App() {
function showMess(text) {
alert(text);
}
return <div>
<button onClick={() => showMess('user1')}>show1</button>
<button onClick={() => showMess('user2')}>show2</button>
</div>;
}
There are three buttons given:
function App() {
return <div>
<button>act1</button>
<button>act2</button>
<button>act3</button>
</div>;
}
Make it so that when you click on the first button through alert
the number 1
is displayed, when you click on the second button the number 2
is displayed, and when you click on the third button the number 3
is displayed.