current position:Home>In react, the click event with parameters will be executed immediately

In react, the click event with parameters will be executed immediately

2022-11-24 20:14:12wandoumm

Write the click event in this way, and the event will be executed when the page is initialized

import './App.css';

const App=() => {
const btnAction=(a)=>{
console.log(this,a);
}
return (


click event


);
}

export default App;

Method 1: Change the click event

//When the method remains unchanged, the change event is called with an arrow function
const btnAction=(a)=>{
console.log(a);//underfin XXX
}

btnAction('XXX')}>click event

Method 2: Change the method

//When the method remains unchanged, the change event is called with an arrow function
const btnAction=(a)=>(e)=>{
console.log(a);//underfin XXX
}

Click event

Using the function method is the same

//When the event remains unchanged, it is also possible to change the method
function btnAction(a){
console.log(a);//underfin XXX
}
return (


btnAction('XXX')}>Click event


);
}

export default App;

copyright notice
author[wandoumm],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/328/202211242005324648.html

Random recommended