[Field]自定义组件在unmount后,还是校验了 #3962
2022/7/7 23:20:18
本文主要是介绍[Field]自定义组件在unmount后,还是校验了 #3962,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Comments
cwtuan commented 15 days ago
ComponentField Reproduction linkhttps://riddle.alibaba-inc.com/riddles/27d1ebab import ReactDOM from 'react-dom'; import React from 'react'; import { Input, Button, Checkbox, Field,Form,Select } from '@alifd/next'; const CheckboxGroup = Checkbox.Group; const dataSource=[1,2,3]; const MySelect = (props) => { return <Select {...props}/> } class App extends React.Component { field = new Field(this, {scrollToFirstError: -10}); notEmpty(rule, value) { if (!value || value.length==0) { return Promise.reject("必选"); } else { return Promise.resolve(null); } } render() { const init = this.field.init; return (<div className="demo"> <Form field={this.field}> <Form.Item> <Input {...init('input', { })} /> </Form.Item> { this.field.getValue('input') && <> <Form.Item> <Select multiple dataSource={dataSource} {...init('next_select', { rules: [{validator: this.notEmpty}] })} /> </Form.Item> <Form.Item> <MySelect multiple dataSource={dataSource} {...init('my_select', { rules: [{validator: this.notEmpty}] })} /> </Form.Item> </> } </Form> <br/> <Button type="primary" onClick={() => { this.field.validatePromise().then(({errors, values}) => { console.warn('aaa errors',errors); console.log('aaa values',values) }); }}>validate</Button> </div>); } } ReactDOM.render(<App/>, mountNode); Steps to reproduce
next_select是fusion原生组件,my_select只是包装成自定义组件(但里面也是Fusion原生组件) const MySelect = (props) => { return <Select {...props}/> } |
bindoon commented 15 days ago •
edited
你的组件不支持 ref,需要用 React.forwordRef 包裹下 const MySelect = React.forwordRef((props, ref) => { return <Select {...props} ref={ref}/> }) |
cwtuan commented 15 days ago
那最好在文档描述一下这个问题和解法。 但我觉得这样对使用上很不方便,你们再看看,能否改变实现,不要靠ref |
这篇关于[Field]自定义组件在unmount后,还是校验了 #3962的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-22[开源]10.3K+ Star!轻量强大的开源运维平台,超赞!
- 2024-11-21Flutter基础教程:新手入门指南
- 2024-11-21Flutter跨平台教程:新手入门详解
- 2024-11-21Flutter跨平台教程:新手入门与实践指南
- 2024-11-21Flutter列表组件教程:初学者指南
- 2024-11-21Flutter列表组件教程:新手入门指南
- 2024-11-21Flutter入门教程:初学者必看指南
- 2024-11-21Flutter入门教程:从零开始的Flutter开发指南
- 2024-11-21Flutter升级教程:新手必读的升级指南
- 2024-11-21Flutter升级教程:轻松掌握Flutter版本更新