useEffect(() => { if (!isEmpty(origin)) { const clonedOrigin = _.cloneDeep(origin); clonedOrigin.dataset_group = clonedOrigin.dataset_group.id; if (clonedOrigin.data_type === 'json') { setJsonExample(clonedOrigin.example); } clonedOrigin.example = JSON.stringify(clonedOrigin.example); let tosetValues = { ...clonedOrigin }; const columnSeparator = clonedOrigin?.column_separator; if (Object.values(SeparatorSymbolMap).includes(columnSeparator)) { tosetValues = { ...tosetValues, separator_type: SeparatorType.INVISIBLE, }; } setInitTableData(clonedOrigin?.table); form.setFieldsValue(tosetValues); } }, [origin, form]);优化下这个代码
时间: 2023-12-18 09:05:26 浏览: 168
可以考虑将一些重复的代码提取出来,避免冗余。同时也可以使用解构来简化代码。优化后的代码如下:
```
useEffect(() => {
if (!isEmpty(origin)) {
const { dataset_group, data_type, example, table, column_separator, ...rest } = _.cloneDeep(origin);
rest.dataset_group = dataset_group.id;
if (data_type === 'json') {
setJsonExample(example);
}
rest.example = JSON.stringify(example);
if (Object.values(SeparatorSymbolMap).includes(column_separator)) {
rest.separator_type = SeparatorType.INVISIBLE;
}
setInitTableData(table);
form.setFieldsValue(rest);
}
}, [origin, form]);
```
阅读全文