current position:Home>Several ways of calling methods to transfer parameters in react render

Several ways of calling methods to transfer parameters in react render

2022-06-24 08:18:54Brother Nie

Mode one : Method writing higher order function

getData = (params) => {
    
    return () => {
    
        console.log(params)
    }
}
render() {
    
    return (
        <div>
            <button onClick={
    this.getData(666)}> How to pass parameters </button>
        </div>
    );
}

Mode two : Change the function when calling the function this Point to

getData = (params) => {
    
    console.log(params)
}
render() {
    
  return (
      <div>
          <button onClick={
    this.getData.bind(this, 11)}> How to pass parameters </button>
      </div>
  );
}

Mode three : Write the form of callback when calling

getData = (params) => {
    
   console.log(params)
}
render() {
    
  return (
      <div>
          <button onClick={
    () => this.getData(11)}> How to pass parameters </button>
      </div>
  );
}

copyright notice
author[Brother Nie],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240459169080.html

Random recommended