简体   繁体   English

JSX的onClick属性反应元素无法按预期工作

[英]onClick attribute for JSX react element not working as expected

The onClick attribute for a button element created within a variable is not firing. 在变量中创建的按钮元素的onClick属性未触发。

I tried directly defining the function in the onClick attribute, and simply out-putting to the console, but there is still nothing output to the console, and the button is not working as expected. 我尝试直接在onClick属性中定义函数,并简单地输出到控制台,但是仍然没有输出到控制台,并且按钮没有按预期工作。

I also added a 'disabled={false}' attribute to the button to make sure that was not an issue, but the onClick attribute is still not working as expected. 我还在按钮中添加了“disabled = {false}”属性以确保这不是问题,但onClick属性仍未按预期工作。

let display = (<div className ='dtaskhome'>
    <h1>Tasks</h1>
    <button disabled={false} onClick={newTask} className='btask'>New Task +</button>
    <ul className ='ultasks'>{taskItems}</ul>
</div>);

function newTask() {
   console.log('point reached');
    Tasks.props.state.newTask = true;
}

No error messages related to the onClick attribute are being displayed, and the current code correctly renders the component. 没有显示与onClick属性相关的错误消息,并且当前代码正确呈现该组件。

I also added a 'disabled={false}' attribute to the button to make sure that was not an issue, but the onClick attribute is still not responsive. 我还在按钮中添加了“disabled = {false}”属性以确保这不是问题,但onClick属性仍然没有响应。

Try implementing the function as a method. 尝试将该函数实现为方法。

newTaskHandler = (props) => {
   console.log('point reached');
   props.state.newTask = true;
}

Then try 然后试试

onClick={this.newTask(this.props)}

Hope this helps you. 希望这对你有所帮助。

try to call the function: 尝试调用该函数:

onClick={newTask()}

And if not try to call it with this. 如果没有尝试用它调用它。

onClick={this.newTask()}

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

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