patternjavascriptreactCritical
React onClick function fires on render
Viewed 0 times
functionrenderreactonclickfires
Problem
I pass 2 values to a child component:
I use a .map() function to display my list of objects(like in the example given in react tutorial page), but the button in that component fires the
My question is: why does
- List of objects to display
- delete function.
I use a .map() function to display my list of objects(like in the example given in react tutorial page), but the button in that component fires the
onClick function, on render(it should not fire on render time). My code looks like this:module.exports = React.createClass({
render: function(){
var taskNodes = this.props.todoTasks.map(function(todo){
return (
{todo.task}
Submit
);
}, this);
return (
{taskNodes}
);
}
});My question is: why does
onClick function fire on render and how to make it not to?Solution
Because you are calling that function instead of passing the function to onClick, change that line to this:
{ this.props.removeTaskFunction(todo) }}>Submit=> called Arrow Function, which was introduced in ES6, and will be supported on React 0.13.3 or upper.Code Snippets
<button type="submit" onClick={() => { this.props.removeTaskFunction(todo) }}>Submit</button>Context
Stack Overflow Q#33846682, score: 744
Revisions (0)
No revisions yet.