Last active
March 18, 2016 18:01
-
-
Save samsch/27f82e63a57bb2e118bc to your computer and use it in GitHub Desktop.
React performance problem: Creating a new function on every render, when you need parameters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react'); | |
//This would be passed props like: | |
{ | |
btns: [ | |
{id: 'btn1', label: 'Button 1'}, | |
{id: 'btn2', label: 'Button 2'}, | |
{id: 'btn3', label: 'Button 3'}, | |
], | |
onBtnClick: function(btnId) { | |
//Does something and is probably bound to the parent. | |
} | |
} | |
let BtnList = React.createClass({ | |
render: function() { | |
return ( | |
<ul> | |
{this.props.btns.map((aBtn) => { | |
return ( | |
<li key={aBtn.id} }> | |
{/* The problem is here. This onClick handler will get recreated on every render. */} | |
<button type="button" onClick={() => this.props.onBtnClick(aBtn.id)} >{aBtn.label}</button> | |
</li> | |
); | |
})} | |
</ul> | |
); | |
}, | |
}); | |
module.exports = BtnList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment