field-form
field-form copied to clipboard
The `form.setFields` is causing the warning: "Warning: There may be circular references"
form.setFields
會導致警告「Warning: There may be circular references」
How to reproduce 如何重現
- Visit https://codesandbox.io/p/devbox/rc-field-form-circular-references-8z9hfk?file=%2Fsrc%2FApp.tsx
- Open the "CodeSandbox Preview Console" or the "Browser DevTools Console". 打開 CodeSandbox 或是瀏覽器的開發人員工具
- Click the "Set Form Fields" button on the webpage. 點擊網頁中的「Set Form Fields」按鈕
- Check the warning log in the console. 於 Console(控制台)中查看警告 log(日誌訊息)
Possible causes 可能造成的原因
Both errors
and warnings
point to EMPTY_ERRORS
, causing isEqual
to perform a deepEqual
comparison, determining that warnings
and the errors
already existing in refSet
have the same reference.
errors
與 warnings
皆指向 EMPTY_ERRORS
,造成 isEqual 進行 deepEqual
時,判斷 warnings
與已存在於 refSet
的 errors
有相同的 reference(参考)。
https://github.com/react-component/field-form/blob/294125efc9c97dfcd7217712dfb193c090da7e4b/src/Field.tsx#L148-L149
https://github.com/react-component/field-form/blob/294125efc9c97dfcd7217712dfb193c090da7e4b/src/Field.tsx#L517-L518
https://github.com/react-component/field-form/blob/294125efc9c97dfcd7217712dfb193c090da7e4b/src/Field.tsx#L240-L242
Related Pull Request 相關的 PR
#604
import Form, { Field } from "rc-field-form";
const Input = ({ value = "", ...props }) => <input {...props} value={value} />;
function App() {
const [form] = Form.useForm<{ username: string }>();
const setFormFields = () => {
// Warning: Warning: There may be circular references
// https://github.com/react-component/util/blob/871c2c146b1050e33091005b32b8debcdcd15ca7/src/isEqual.ts#L14
form.setFields([{ name: "username", errors: [] }]);
};
return (
<Form form={form}>
<Field
name="username"
rules={[{ required: true }]}
onMetaChange={() => {}}
>
<Input />
</Field>
<button onClick={setFormFields}>Set Form Fields</button>
</Form>
);
}
export default App;