请问antd中输入框的函数防抖要怎么弄?
这是我写的一个测试代码
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Form, Input } from "antd";
import debounce from "lodash.debounce";
const FormItem = Form.Item;
const CustomizedForm = Form.create({
onFieldsChange(props, changedFields) {
props.onChange(changedFields);
}
})(props => {
const { getFieldDecorator } = props.form;
return (
<Form layout="inline">
<FormItem label="Username">
{getFieldDecorator("username", {
rules: [{ required: true, message: "Username is required!" }]
})(<Input />)}
</FormItem>
</Form>
);
});
class Demo extends React.Component {
state = {
fields: {
username: {
value: "benjycui"
}
}
};
handleFormChange = changedFields => {
if (changedFields["username"].validating === false) {
debounce(() => {
console.log("hahaha");
this.setState(({ fields }) => ({
fields: { ...fields, ...changedFields }
}));
}, 2000)();
}
};
render() {
const fields = this.state.fields;
return (
<div>
<CustomizedForm {...fields} onChange={this.handleFormChange} />
<pre className="language-bash">{JSON.stringify(fields, null, 2)}</pre>
<button onClick={this.xxx}>click</button>
</div>
);
}
}
ReactDOM.render(<Demo />, document.getElementById("container"));
https://codesandbox.io/s/k3oq...