首页 > 解决方案 > React-admin - 在渲染不同的组件(`SelectInput`)时无法更新组件(`xxx`)

问题描述

反应管理员 3.8.4

我正在有条件地渲染一些表单字段,并且这些字段有一些验证。因此,我在下面收到此错误:

Warning: Cannot update a component (nameOfTheComponent) while rendering a different component
(SelectInput). To locate the bad setState() call inside SelectInput, follow the stack trace
as described in...

我已经阅读了有关该问题的一些解释,并且发现在注册这些字段时 react-final-form 调用了 setState() ,这似乎是问题所在。

我还看到 FileConfig 中有一个名为静默的修复程序,可以解决这个问题React final form silent

但我不知道我是否用错了,因为警告仍然出现。

我正在尝试做这样的事情:

const OfferVariation = ({ formData, ...rest }) => {
  const form = useForm();

  useEffect(() => {
    return () => {
      const initialState = {}
      let inConstructor = true
      const fieldName = "internalOffer.type"

      form.registerField(fieldName, fieldState => {
        if (inConstructor) {
          initialState[fieldName] = fieldState
        } else {
          this.setState({ [fieldName]: fieldState })
        }
      }, { silent: true })
    }
  }, [])

    if (flowType === "Interna") {
      return (
          <SelectInput
            source="internalOffer.type"
            label="Tipo da Oferta"
            choices={offerTypes}
            validate={validateArrayNotEmpty}
            {...rest}
          />
      )
    } else if (flowType === "Externa") {
      return (
        <TextInput
          label="Url Externa"
          source="externalOffer.externalURL"
          {...rest}
        />
      )
    }
  }
};

export default OfferVariation;

有谁知道如何解决它并可以帮助我?

标签: react-adminreact-final-form

解决方案


推荐阅读