优化以下代码:<div className="groupInfor gd-group"> <div className="header"> <h5>公司股东</h5> </div> <Form.List name="companyList" shouldUpdate={(prevValues, curValues) => prevValues.percent !== curValues.percent }> {(fields, { add, remove }) => ( <> {fields.map(({ key, name, fieldKey, ...restField }, idx) => ( <AddCompany key={key} idx={idx} name={name} fieldKey={fieldKey} isDisabled={isDisabled} restField={restField} remove={remove} form={form} /> ))} <Form.Item > <Button className="new-init-btn offset-t20" onClick={handleAddCompany} style={{ width: 88 }} disabled={isDisabled} >+ 新增</Button> </Form.Item> </> )} </Form.List> </div>
时间: 2024-03-10 16:43:23 浏览: 72
Here are a few optimizations that can be made to this code:
1. Use concise arrow function syntax for the `shouldUpdate` prop of `Form.List`:
```
shouldUpdate={(prevValues, curValues) => prevValues.percent !== curValues.percent}
```
can be simplified to:
```
shouldUpdate={(prev, cur) => prev.percent !== cur.percent}
```
2. Extract the contents of the `Form.List` render prop function into a separate component:
```
{(fields, { add, remove }) => (
<>
{fields.map(({ key, name, fieldKey, ...restField }, idx) => (
<AddCompany key={key} idx={idx} name={name} fieldKey={fieldKey} isDisabled={isDisabled} restField={restField} remove={remove} form={form} />
))}
<Form.Item>
<Button className="new-init-btn offset-t20" onClick={handleAddCompany} style={{ width: 88 }} disabled={isDisabled}>
+ 新增
</Button>
</Form.Item>
</>
)}
```
can be extracted into a component called `CompanyList`:
```
const CompanyList = ({ fields, isDisabled, remove, form, handleAddCompany }) => (
<>
{fields.map(({ key, name, fieldKey, ...restField }, idx) => (
<AddCompany key={key} idx={idx} name={name} fieldKey={fieldKey} isDisabled={isDisabled} restField={restField} remove={remove} form={form} />
))}
<Form.Item>
<Button className="new-init-btn offset-t20" onClick={handleAddCompany} style={{ width: 88 }} disabled={isDisabled}>
+ 新增
</Button>
</Form.Item>
</>
);
// Usage:
<Form.List name="companyList" shouldUpdate={(prev, cur) => prev.percent !== cur.percent}>
{(fields, { add, remove }) => (
<CompanyList fields={fields} isDisabled={isDisabled} remove={remove} form={form} handleAddCompany={handleAddCompany} />
)}
</Form.List>
```
By extracting the contents of the `Form.List` render prop function into a separate component, the code becomes more modular and easier to read and maintain.
阅读全文