const ViewRender = memo((props: ViewProps) => { const { pointData, pageData, width, dragStop, onDragStart, onResizeStop, preview } = props; const FixedData: Array<PointDataItem> = pointData.filter(e => e.item.type == 'Fixed') const nFixedData: Array<PointDataItem> = pointData.filter(e => e.item.type != 'Fixed') return ( (!preview ? ( <GridLayout cols={24} rowHeight={2} margin={[0, 0]} width={width} // @ts-expect-error onDragStop={debounce(dragStop, 500)} onDragStart={onDragStart} onResizeStop={onResizeStop} style={{ minHeight: '100vh', backgroundSize: '100%', backgroundRepeat: 'no-repeat', backgroundColor: pageData && pageData.bgColor, backgroundImage: pageData && pageData.bgImage ? `url(${pageData.bgImage[0].url})` : 'initial' }} > {pointData.map((value: PointDataItem) => ( <div key={value.id} data-grid={value.point} className={onDragStart && styles.dragItem} style={{ overflow: value.item.type == 'Fixed' ? 'visible' : 'hidden' }}> {value.item.type == 'Fixed' ? ( <div style={{ transform: `translateY(-${value.item.config.y}px)` }}> <DynamicEngine {...(value.item as any)} isTpl={false} /> </div> ) : ( <DynamicEngine {...(value.item as any)} isTpl={false} /> )} </div> ))} </GridLayout> ) : ( <> <GridLayout cols={24} rowHeight={2} margin={[0, 0]} width={width} style={{ minHeight: '100vh', backgroundSize: '100%', backgroundRepeat: 'no-repeat', backgroundColor: pageData && pageData.bgColor, backgroundImage: pageData && pageData.bgImage ? `url(${pageData.bgImage[0].url})` : 'initial' }}> {nFixedData.map((value: PointDataItem) => ( <div key={value.id} data-grid={value.point}> <DynamicEngine {...(value.item as any)} isTpl={false} /> </div> ))} </GridLayout> {FixedData.map((value: PointDataItem) => ( <div key={value.id} data-grid={value.point}> <DynamicEngine {...(value.item as any)} isTpl={false} /> </div> ))} </> )) ) })帮我优化这段代码
时间: 2024-02-14 22:09:07 浏览: 128
const T vs T const
这段代码可以进行一些小的优化,如下:
1. 可以将`const { pointData, pageData, width, dragStop, onDragStart, onResizeStop, preview } = props;`解构放在函数参数中,这样可以避免在函数体中使用`props.`的方式来获取参数。
2. 可以将`pointData.map((value: PointDataItem) => (`中的`value`改为`item`,以更好地表达其含义。
3. 可以将`value.item`改为`item`,在整个代码中保持一致。
4. 可以将`value.item.type`改为`itemType`,以更好地表达其含义。
5. 可以使用可选链运算符`?.`来避免出现`pageData`或`pageData.bgImage`为空时的报错。
6. 可以将`onDragStop={debounce(dragStop, 500)}`中的`500`提取成一个常量或变量。
7. 可以将`<DynamicEngine {...(value.item as any)} isTpl={false} />`提取出来成为一个变量,以避免重复代码。
综上所述,代码可以改写为:
```
const ViewRender = memo(({ pointData, pageData, width, dragStop, onDragStart, onResizeStop, preview }: ViewProps) => {
const { bgColor = '', bgImage = [] } = pageData || {};
const FixedData: Array<PointDataItem> = pointData.filter(e => e.itemType === 'Fixed');
const nFixedData: Array<PointDataItem> = pointData.filter(e => e.itemType !== 'Fixed');
const dynamicEngine = (item: any) => (
<DynamicEngine {...(item as any)} isTpl={false} />
);
return (
(!preview ? (
<GridLayout
cols={24}
rowHeight={2}
margin={[0, 0]}
width={width}
onDragStop={debounce(dragStop, 500)}
onDragStart={onDragStart}
onResizeStop={onResizeStop}
style={{
minHeight: '100vh',
backgroundSize: '100%',
backgroundRepeat: 'no-repeat',
backgroundColor: bgColor,
backgroundImage: bgImage[0]?.url || 'initial',
}}
>
{pointData.map(({ id, point, itemType, item }) => (
<div key={id} data-grid={point} className={onDragStart && styles.dragItem} style={{
overflow: itemType === 'Fixed' ? 'visible' : 'hidden',
}}>
{itemType === 'Fixed' ? (
<div style={{ transform: `translateY(-${item?.config?.y}px)` }}>
{dynamicEngine(item)}
</div>
) : (
dynamicEngine(item)
)}
</div>
))}
</GridLayout>
) : (
<>
<GridLayout
cols={24}
rowHeight={2}
margin={[0, 0]}
width={width}
style={{
minHeight: '100vh',
backgroundSize: '100%',
backgroundRepeat: 'no-repeat',
backgroundColor: bgColor,
backgroundImage: bgImage[0]?.url || 'initial',
}}>
{nFixedData.map(({ id, point, item }) => (
<div key={id} data-grid={point}>
{dynamicEngine(item)}
</div>
))}
</GridLayout>
{FixedData.map(({ id, point, item }) => (
<div key={id} data-grid={point}>
{dynamicEngine(item)}
</div>
))}
</>
))
);
});
```
阅读全文