Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

动态 form 中 name 不变时, getFieldProps 先于 saveRef 触发, 导致 fieldMeta 为 undefined, 引发报错 #53

Closed
sandwich99 opened this issue Jan 3, 2017 · 2 comments · Fixed by #117

Comments

@sandwich99
Copy link

错误:

image

Form 由于使用了高阶组件, 先调用了 getFieldProps , 包含初始化 fieldMeta

fieldMeta = fieldsMeta[name] = fieldsMeta[name] || {};

rc-form 重载了 ref

inputProps.ref = this.getCacheBind(name, `${name}__ref`, this.saveRef);

该方法会在 component unmount 时调用

image

saveRef 清理了 meta state

      saveRef(name, _, component) {
        if (!component) {
          // after destroy, delete data
          delete this.fieldsMeta[name];
          delete this.fields[name];
          delete this.instances[name];
          delete this.cachedBind[name];
          return;
        }
        const fieldMeta = this.getFieldMeta(name); // 第二次调用时 由于 fieldMeta 为空, 所以并不会执行 ref(component)
        if (fieldMeta) {
          const ref = fieldMeta.ref;
          if (ref) {
            if (typeof ref === 'string') {
              throw new Error(`can not set ref string for ${name}`);
            }
            ref(component);
          }
        }
        this.instances[name] = component;
      },

在 react attachRef时, saveRef 会再次被调用, 但此时 fieldMeta 已经在 unmount 时被删除了
image

所以此时 ref(component) 不会被触发, 导致报错

image

@benjycui
Copy link
Member

benjycui commented Jan 5, 2017

此 issue 关闭,跟进 antd 中的那个即可。

@kala888
Copy link

kala888 commented Jan 22, 2018

类似问题,在使用react-weui的时候,使用weui的Input会报同样的错

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants