form
form copied to clipboard
class组件用hook组件重写,componentWillMount()生命周期里的代码写在哪里?
class组件用hook组件重写,componentWillMount()生命周期里的代码写在哪里? 比如下面示例:
import { createForm } from 'rc-form';
class Form extends React.Component {
componentWillMount() {
this.requiredDecorator = this.props.form.getFieldDecorator('required', {
rules: [{required: true}],
});
}
submit = () => {
this.props.form.validateFields((error, value) => {
console.log(error, value);
});
}
render() {
let errors;
const { getFieldError } = this.props.form;
return (
<div>
{this.requiredDecorator(
<input
onChange={
// can still write your own onChange
}
/>
)}
{(errors = getFieldError('required')) ? errors.join(',') : null}
<button onClick={this.submit}>submit</button>
</div>
);
}
}
export createForm()(Form);