I have a ul li list inside the render method and there is an onClick event on li which call this.handleClick function and state gets changed
var InspirationFilter = React.createClass({
getInitialState: function() {
return {currentFilterText:"Top All Time", currentFilter:"top_all_time", showUl:false};
},
handleClick: function(filter, filterText){
this.setState({currentFilterText:filterText, currentFilter:filter, showUl:!this.state.showUl});
},
render: function() {
return (
<ul>
<li onClick={this.handleClick('top_all_time', 'Top All Time')}>Top All Time</li>
<li onClick={this.handleClick('top_this_week', 'Top This Week')}>Top Week</li>
<li onClick={this.handleClick('top_this_month', 'Top This Month')}>Top Month</li>
</ul>
);
}
});
But this code gives me the error
Warning: setState(...): Cannot update during an existing state transition (such as within
render). Render methods should be a pure function of props and state
I tried to use bind with the click event like this,
return (
<ul>
<li onClick={this.handleClick.bind(this,'top_all_time', 'Top All Time')}>Top All Time</li>
<li onClick={this.handleClick.bind(this,'top_this_week', 'Top This Week')}>Top Week</li>
<li onClick={this.handleClick.bind(this.'top_this_month', 'Top This Month')}>Top Month</li>
</ul>
);
The above error is gone now but ran into another error which is as follows,
Warning: bind(): You are binding a component method to the component. React does this for you automatically in a high-performance way, so you can safely remove this call. See InspirationFilter
In the react documentation Communicate Between Components they are also using the bind method.
Any suggestions to fix it?
lihas a.afterthisinstead of,- is that in your source code or just here?(dot). Now the click event is working but i am still having the warning. Is there is any better way to do it without warning?React v0.14.1nullinstead ofthiswhen you are binding your parameters