How React constructor binding events pass parameters

class albumList extends PureComponent {
    constructor(props) {
        super(props)
        this.play = this.play.bind(this)
    }
    play(song) {
        console.log(song)
    }
    render() {
        return(
            <div className="album-list">
                {
                    this.props.data.map((item,index)=>{
                        return (
                            <div className="album-list-item" key={index}>
                                    <img onClick={this.play } alt="" src="./img/album-list-play.png" />
                            </div>
                        )
                    })
                }
            </div>
        );
    }
}

in the above code, the event play is bound in the way of bind in the constructor, but it seems that it can"t get through the parameters. Binding the event in the constructor is the recommended optimization scheme in react, and if you want to pass parameters, how do you write it?

how react binds events:

  1. binds the method directly to the bind in the element, but each time render triggers the function
  2. use the arrow function in the element so that you can pass parameters, but you will find that this is just putting the same function in different memory
Mar.17,2021

Hey, if you don't want to use anonymous functions, you can abstract img to a layer of components for reference:

  https://www.npmjs.com/package.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e8ab67-ffc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e8ab67-ffc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?