alibaba / formily

📱🚀 🧩 Cross Device & High Performance Normal Form/Dynamic(JSON Schema) Form/Form Builder -- Support React/React Native/Vue 2/Vue 3
https://formilyjs.org/
MIT License
11.48k stars 1.49k forks source link

[Bug Report] 自定义组件无法触发阅读态 #3412

Closed panghaoyuan closed 2 years ago

panghaoyuan commented 2 years ago

[ ] I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

复现demo https://github.com/panghaoyuan/formily-bug-demo

Steps to reproduce

git clone https://github.com/panghaoyuan/formily-bug-demo.git
pnpm i 
pnpm dev

What is expected?

自定义组件可以触发阅读态

What is actually happening?

自定义组件无法触发阅读态

Package

@formily/react@2.2.5


通过源码断点发现问题,但是无法解决问题,希望以下代码有参考价值 自定义组件中 useField()null

export function mapReadPretty(component, readPrettyProps) {
    return function (target) {
        return observer(function (props) {
            var field = useField(); // 自定义组件field为null
            if (!isVoidField(field) && (field === null || field === void 0 ? void 0 : field.pattern) === 'readPretty') {
                return React.createElement(component, __assign(__assign({}, readPrettyProps), props));
            }
            return React.createElement(target, props);
        }, {
            forwardRef: true,
        });
    };
}

https://github.com/alibaba/formily/blob/23b94cdb026f492d65bb5d8234e5d6e52dd22ae7/packages/react/src/shared/connect.ts#L47-L68

janryWang commented 2 years ago

应该是打包问题,重复打包了,react context拿不到

panghaoyuan commented 2 years ago

@janryWang 我提供的issues里提供最小实现代码了,这个问题怎么解决,大佬能给个思路吗,或者帮我解决一下吗

panghaoyuan commented 2 years ago

@janryWang 找到问题了,,不是重复打包,是pnpm的包版本没对齐导致的问题,thank you.