hoc修饰过后的Input组件在Form中的使用
- 前置条件
- React@ V < 16.3
- 需求
- 对Form表单中Input组件进行全部只读/局部只读状态设置
- 解决方法
-
context + hoc
1.```jsx
import React, {createContext} from 'react'const defaultContext = {
disabled: false,
// setDisabled: () => {},
}export const {Provider, Consumer} = createContext(defaultContext)
const WithFormStatusHoc = (_component) => {
const Component = _component
return (props, ref) => {
return (
{({disabled}) => (
<Component disabled={disabled} {...props} />
)}
)
}
}export default WithFormStatusHoc
``` -
hoc修饰
1.```jsx
import {Col, Row, Form, Card, Input as AntInput, Select as AntSelect} from 'antd'const {Option} = AntSelect
const Input = forwardRef(WithFormStatusHoc(AntInput))
const Select = forwardRef(WithFormStatusHoc(AntSelect))
``` -
通过 Provider 来设置 hoc在表单中生效的范围
-
- 关注点
- 经hoc修饰后的组件为自定义控件,需要forwardRef来传递ref

- 经hoc修饰后的组件为自定义控件,需要forwardRef来传递ref

浙公网安备 33010602011771号