Warning: [antd: Switch] value is not a valid prop, do you mean checked?
Warning: React does not recognize the valuePropName prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase valuepropname instead. If you accidentally passed it from a parent component, remove it from the DOM element.

React不能识别dom元素上的非标准attribute报出的警告,最终的渲染结果中React会移除这些非标准的attribute
用 rest接收属性参数,仅将 rest属性参数传递给子组件或对应的dom,自定义属性只组件自己使用

  1. async function onSubmit(){
  2. const values = form.getFieldsValue() // 不校验表单
  3. // 校验表单
  4. const formValues = await form.validateFields();
  5. }
  6. const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
  7. async function init() {
  8. await sleep(100)
  9. }

formInstance 属性

  1. [
  2. "getFieldValue",
  3. "getFieldsValue",
  4. "getFieldError",
  5. "getFieldWarning",
  6. "getFieldsError",
  7. "isFieldsTouched",
  8. "isFieldTouched",
  9. "isFieldValidating",
  10. "isFieldsValidating",
  11. "resetFields",
  12. "setFields",
  13. "setFieldValue",
  14. "setFieldsValue",
  15. "validateFields",
  16. "submit",
  17. "_init",
  18. "getInternalHooks",
  19. "__INTERNAL__",
  20. "scrollToField",
  21. "getFieldInstance"
  22. ]

rules验证

  • required 是否必填项,Boolean
  • message 提示信息,String
  • type 内置校验类型,String
  • validator 自定义校验函数,Function
  • pattern 正则表达式校验,Regexp
  • len 字段长度,Number
  • max 最大长度,Number
  • min 最小长度,Number
  • whitespace 空格是否会被视为错误,Boolean

滚动到错误的位置

  1. function App() {
  2. function valuesChange() {
  3. form.validateFields(err => {
  4. if (err) {
  5. const scroll = {
  6. alignWithTop: true,
  7. onlyScrollIfNeeded: true,
  8. allowHorizontalScroll: false,
  9. };
  10. form.validateFieldsAndScroll({ scroll });
  11. }
  12. });
  13. }
  14. return (
  15. <Form />
  16. )
  17. }