fix: performance optimizations

This commit is contained in:
chenos 2023-06-15 11:27:11 +08:00
parent c4816c6963
commit c89d7f980f

View File

@ -28,18 +28,20 @@ export const AssociationFieldProvider = observer(
[fieldSchema['x-component-props']?.mode],
);
const targetKeyValue = useMemo(() => {
if (!field.value) return '';
if (['belongsTo', 'hasOne'].includes(collectionField.type)) {
return field.value[collectionField.targetKey] ?? '';
}
if (['belongsToMany', 'hasMany'].includes(collectionField.type)) {
if (Array.isArray(field.value)) {
return field.value.map((v) => v[collectionField.targetKey] ?? '').join(',');
}
}
return '';
}, [collectionField, field.value]);
// const targetKeyValue = useMemo(() => {
// if (!field.value) return '';
// if (['belongsTo', 'hasOne'].includes(collectionField.type)) {
// return field.value[collectionField.targetKey] ?? '';
// }
// if (['belongsToMany', 'hasMany'].includes(collectionField.type)) {
// if (Array.isArray(field.value)) {
// return field.value.map((v) => v[collectionField.targetKey] ?? '').join(',');
// }
// }
// return '';
// }, [collectionField, field.value]);
const fieldValue = useMemo(() => JSON.stringify(field.value), [field.value]);
const [loading, setLoading] = useState(true);
@ -84,7 +86,7 @@ export const AssociationFieldProvider = observer(
}
setLoading(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentMode, collectionField, targetKeyValue]);
}, [currentMode, collectionField, fieldValue]);
if (loading) {
return null;