Skip to content

Commit

Permalink
fix: 修复死循环问题
Browse files Browse the repository at this point in the history
  • Loading branch information
JackySoft committed Oct 30, 2024
1 parent 9718633 commit 261a431
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/editor/src/packages/FormItems/CheckBox/CheckBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ const MCheckBox = ({ id, type, config, onChange }: ComponentType<IConfig>, ref:
const [disabled, setDisabled] = useState(false);
const { form, formId, setFormData } = useFormContext();
const variableData = usePageStore((state) => state.page.variableData);
// 初始化默认值

/**
* 初始化默认值
* 此处需要注意:默认值可能是一个数组,必须比对字符串,否则会出现死循环
*/
useEffect(() => {
const name: string = config.props.formItem?.name;
const value = config.props.defaultValue || [];
if (name && !isNull(value)) {
form?.setFieldValue(name, value);
setFormData({ name: formId, value: { [name]: value } });
}
}, [config.props.defaultValue]);
}, [JSON.stringify(config.props.defaultValue)]);

// 启用和禁用
useEffect(() => {
Expand Down
8 changes: 6 additions & 2 deletions packages/materials/FormItems/CheckBox/CheckBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ const MCheckBox = ({ config, onChange }: ComponentType<IConfig>, ref: any) => {
const [disabled, setDisabled] = useState(false);
const { form, formId, setFormData } = useFormContext();
const variableData = usePageStore((state) => state.page.variableData);
// 初始化默认值

/**
* 初始化默认值
* 此处需要注意:默认值可能是一个数组,必须比对字符串,否则会出现死循环
*/
useEffect(() => {
const name: string = config.props.formItem?.name;
const value = config.props.defaultValue || [];
if (name && !isNull(value)) {
form?.setFieldValue(name, value);
setFormData({ name: formId, value: { [name]: value } });
}
}, [config.props.defaultValue]);
}, [JSON.stringify(config.props.defaultValue)]);

// 启用和禁用
useEffect(() => {
Expand Down

0 comments on commit 261a431

Please sign in to comment.