简体   繁体   English

在内部渲染中反应Js onClick

[英]React Js onClick inside render

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 我有一个ul li内部列表render方法和存在onClick事件li哪个呼叫this.handleClick功能和状态得到改变

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 ). 警告:setState(...):在现有状态转换期间(例如在render )无法更新。 Render methods should be a pure function of props and state 渲染方法应该是道具和状态的纯函数

I tried to use bind with the click event like this, 我尝试使用bind这样的click事件,

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. 警告:bind():您正在将组件方法绑定到组件。 React does this for you automatically in a high-performance way, so you can safely remove this call. React会以高性能方式自动为您执行此操作,因此您可以安全地删除此调用。 See InspirationFilter 见InspirationFilter

In the react documentation Communicate Between Components they are also using the bind method. 在反应文档中, 在组件之间进行通信时,他们也使用bind方法。

Any suggestions to fix it? 有什么建议可以解决吗?

The problem is that onClick value has to be a function, so just create a helper method that creates such a function for you: 问题是onClick值必须是一个函数,所以只需创建一个帮助器方法,为您创建这样的函数:

createClickHandler: function(filter, filterText){
    return function() {
        this.setState({...});
    }.bind(this);
},

render: function() {
    return (
        <ul>
            <li onClick={this.createClickHandler('top_all_time', 'Top All Time')}>Top All Time</li>
            <li onClick={this.createClickHandler('top_this_week', 'Top This Week')}>Top Week</li>
            <li onClick={this.createClickHandler('top_this_month', 'Top This Month')}>Top Month</li>
        </ul>
    );
}

React is really just a bit over-helpful here with its warning. React实际上只是有点过于乐于助人的警告。 Your reason for using bind() is no to bind this , but rather to bind the arguments. 你使用bind()原因是不绑定this ,而是绑定参数。

Why it works? 为什么会这样? This solution avoids the re-binding warning because it's not re-binding an already bound this.handleClick , but instead creating a new function (that's not yet bound to anything) and binding that. 这个解决方案避免了重新绑定警告,因为它没有重新绑定已绑定的this.handleClick ,而是创建一个新函数(尚未绑定到任何东西)并绑定它。

To summarize, in your first example, when you use this format: 总而言之,在您的第一个示例中,当您使用以下格式时:

onClick={this.functionName(arg1, arg2)

you are calling the functions rather than providing a reference to the functions. 您正在调用函数而不是提供对函数的引用。 Hence they are being called directly every time it is being rendered rather than onClick as intended. 因此,每次渲染时都会直接调用它们,而不是按预期方式单击。

When you use the format: 使用格式时:

onClick={this.functionName.bind(this, arg1, arg2)}

you are providing a reference to the function and binding context and arguments, rather than calling it. 您提供了对函数和绑定上下文和参数的引用,而不是调用它。 This is the correct method; 这是正确的方法; ignore the warnings (even though there are way too many and it's really annoying) 忽略警告(即使有太多的东西,它真的很烦人)

Yes, you do need to pass a function to onClick . 是的,您需要将函数传递给onClick

You have a typo on the third <li> 你有第三个<li>的拼写错误

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>
);

Check this Fiddle https://jsfiddle.net/69z2wepo/20047/ It works on React 0.14 检查这个小提琴https://jsfiddle.net/69z2wepo/20047/它适用于React 0.14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM