为什么我的 onClick 在渲染时被调用?- 反应.js
2022-08-30 04:28:31
我有一个我创建的组件:
class Create extends Component {
constructor(props) {
super(props);
}
render() {
var playlistDOM = this.renderPlaylists(this.props.playlists);
return (
<div>
{playlistDOM}
</div>
)
}
activatePlaylist(playlistId) {
debugger;
}
renderPlaylists(playlists) {
return playlists.map(playlist => {
return <div key={playlist.playlist_id} onClick={this.activatePlaylist(playlist.playlist_id)}>{playlist.playlist_name}</div>
});
}
}
function mapStateToProps(state) {
return {
playlists: state.playlists
}
}
export default connect(mapStateToProps)(Create);
当我这个页面时,被调用在我的.如果我喜欢:render
activatePlaylist
playlist
map
bind
activatePlaylist
activatePlaylist.bind(this, playlist.playlist_id)
我也可以使用一个匿名函数:
onClick={() => this.activatePlaylist(playlist.playlist_id)}
然后它按预期工作。为什么会发生这种情况?