useFieldSchema
Description
Mainly used inside custom components to read the Schema information of the current field. This hook can only be used inside the subtree of SchemaField or RecursionField.
Schema Protocol Notes
This page only explains how to read the current field's Schema in @silver-formily/react. For the complete documentation of Schema, ISchema and the various protocol fields, please refer to the JSON Schema documentation. The on-site bridge entry is at Schema.
Signature
import type { Schema } from '@silver-formily/json-schema'
declare function useFieldSchema(): SchemaSee Schema for the Schema type bridging notes, and the JSON Schema documentation for the full API.
Usage
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>
)
}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>
)
}
This hook reads context; it does not create a model or subscribe to observable properties. Use the corresponding provider. Although some signatures are non-nullable, missing or cleared context can return null/undefined at runtime.