useFieldSchema
描述
主要在自定义组件中读取当前字段的 Schema 信息,该 hook 只能用在 SchemaField 或者 RecursionField 的子树中使用
Schema 协议说明
本页只说明 @silver-formily/react 中如何读取当前字段的 Schema。Schema、ISchema 和各类协议字段的完整文档请查看 JSON Schema 文档。当前站内的桥接入口见 Schema。
签名
ts
import type { Schema } from '@silver-formily/json-schema'
declare function useFieldSchema(): SchemaSchema 类型桥接说明见 Schema,完整 API 请查看 JSON Schema。
用例
tsx
import { createForm } from '@silver-formily/core'
import {
createSchemaField,
FormProvider,
useFieldSchema,
} from '@silver-formily/react'
const form = createForm({ validateFirst: true })
// useFieldSchema 只能在 SchemaField 或 RecursionField 子树内使用
function Custom() {
const schema = useFieldSchema()
return (
<div style={{ whiteSpace: 'pre' }}>
{JSON.stringify(schema?.toJSON(), null, 4)}
</div>
)
}
const SchemaField = createSchemaField({
components: {
Custom,
},
})
export default function Demo() {
return (
<FormProvider form={form}>
<SchemaField>
<SchemaField.Object
name="custom"
x-component="Custom"
x-component-props={{
schema: {
type: 'object',
properties: {
input: {
'type': 'string',
'x-component': 'Custom',
},
},
},
}}
/>
</SchemaField>
</FormProvider>
)
}tsx
import { createForm } from '@silver-formily/core'
import {
createSchemaField,
FormProvider,
useFieldSchema,
} from '@silver-formily/react'
const form = createForm({ validateFirst: true })
// useFieldSchema 只能在 SchemaField 或 RecursionField 子树内使用
function Custom() {
const schema = useFieldSchema()
return (
<div style={{ whiteSpace: 'pre' }}>
{JSON.stringify(schema?.toJSON(), null, 4)}
</div>
)
}
const SchemaField = createSchemaField({
components: {
Custom,
},
})
export default function Demo() {
return (
<FormProvider form={form}>
<SchemaField>
<SchemaField.Object
name="custom"
x-component="Custom"
x-component-props={{
schema: {
type: 'object',
properties: {
input: {
'type': 'string',
'x-component': 'Custom',
},
},
},
}}
/>
</SchemaField>
</FormProvider>
)
}
查看源码
该 hook 只读取上下文,不创建模型,也不订阅模型属性。应在对应 Provider 下使用;即使部分签名未声明空值,缺少或被清理的上下文在运行时仍可能返回 null/undefined。