reactjs - Rapid onChange 导致 OnClick 不注册
问题描述
我在 onChange 输入字段时设置状态,然后在 onClick 上有一个提交功能。如果我在最后一次输入后一秒钟左右单击它,则 onClick 根本不会注册。
我已经从不需要的组件中删除了所有内容,并留下了这个:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import Loader from '../utils/Loader';
import './CustomMenu.scss';
import {custom_getCategory} from '../../actions/customActions';
import propsDebug from '../utils/propsDebug';
class CustomMenu extends Component {
constructor(props) {
super(props);
this.state = {
input: "",
customWords: [],
}
this.formToState = this.formToState.bind(this);
this.submit = this.submit.bind(this);
}
formToState(e) {
const {value, name} = e.target;
this.setState({[name]: value});
}
submit() {
const {input} = this.state;
this.setState( state => {
state.input = "";
console.log("newState", state);
return state;
});
}
render() {
const {input, customWords} = this.state;
return (
<div className="CustomMenu">
<input name="input" value={input} onChange={(e) => this.formToState(e)}/>
<button onClick={(e) => {
console.log("onClick");
this.submit(e);
}} style={{margin: "10px"}}>Add</button>
</div>
)
}
}
const mapStateToProps = (state, ownProps) => ({
data: state[ownProps.reduxState],
custom: state.custom,
})
export default connect(mapStateToProps, {custom_getCategory})(CustomMenu);
有什么想法可以解决这个问题吗?我觉得我以前多次使用这种模式没有问题 - 我不知道我错过了什么。
解决方案
<button onClick={(e) => (
console.log("onClick");
this.submit(e);
)} style={{margin: "10px"}}>Add</button>
改为使用隐式返回。
推荐阅读
- javascript - 在同一脚本标签中编写另一个方法后,Javascript 方法停止工作
- laravel-5 - Laravel 使用关系和分页对数据进行排序
- laravel - laravel-echo-server - 客户端无法通过身份验证,获得 HTTP 状态 403 JWT 身份验证
- javascript - 尝试移动页面时重定向不起作用
- flutter - 点击效果不流畅
- c# - .net 核心:模型中的 ICollection 始终为空
- html - 像“src =”这样的html部分是什么?
- algorithm - 如何快速检查有向图的边缘是一座坚固的桥梁
- php - 将 Wordpress 用户管理迁移到自定义 CMS
- android - 从android中的服务器下载内容时屏幕冻结