useExpressionScope
Description
Reads the Schema expression scope of the current subtree. Variables injected by createSchemaField and ExpressionScope can all be read in custom components via this hook.
Signature
ts
declare function useExpressionScope(): anyUsage
tsx
import { ExpressionScope, useExpressionScope } from '@silver-formily/react'
import { Typography } from 'antd'
// 读取当前子树的表达式作用域,常用于自定义组件消费 scope 中的变量
function Custom() {
const scope = useExpressionScope() as { text?: string } | undefined
return (
<Typography.Text>
作用域中的 text:
{scope?.text ?? '未注入'}
</Typography.Text>
)
}
export default function Demo() {
return (
<div>
<div style={{ marginBottom: 8 }}>
<Custom />
</div>
<ExpressionScope value={{ text: '来自 ExpressionScope 的值' }}>
<Custom />
</ExpressionScope>
</div>
)
}tsx
import { ExpressionScope, useExpressionScope } from '@silver-formily/react'
import { Typography } from 'antd'
// 读取当前子树的表达式作用域,常用于自定义组件消费 scope 中的变量
function Custom() {
const scope = useExpressionScope() as { text?: string } | undefined
return (
<Typography.Text>
作用域中的 text:
{scope?.text ?? '未注入'}
</Typography.Text>
)
}
export default function Demo() {
return (
<div>
<div style={{ marginBottom: 8 }}>
<Custom />
</div>
<ExpressionScope value={{ text: '来自 ExpressionScope 的值' }}>
<Custom />
</ExpressionScope>
</div>
)
}
查看源码
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.