我正在尝试使用React useEffect
将一个项目添加到我的数组中,但是这个过程重复了两次,我不明白为什么,请看下面的代码:
useEffect(() => {
actionColumn()
}
,[])
这是我的actionColumn函数:
const actionColumn = () => {
columns.push(
{
dataField: 'actions',
text: actionConfig.text,
sort: false,
formatter: (row, cell) => {
return (
<UncontrolledDropdown>
<DropdownToggle tag="button" className="btn btn-link arrow-none card-drop p-0">
<i className="mdi mdi-dots-horizontal" style={{ fontSize: 25, color: "#6c757d" }}></i>
</DropdownToggle>
<DropdownMenu right>
{actionConfig.itens.map(e => {
return (<DropdownItem onClick={() => e.myEvent(cell)}><i className={e.icon}></i>{e.label}</DropdownItem>)
})}
</DropdownMenu>
</UncontrolledDropdown>
)
}
})
}
这是我的错误结果:
#EDIT 1 完整的组件代码:
const RelevoTable = ({
records,
columns,
hasPagination = true,
sorted, hasSearch = false,
exportCsv = false,
bordered = false,
hasToogle = false,
isCondensed = false,
actionConfig = null,
}) => {
useEffect(() => {
if(actionConfig)
actionColumn()
}
,[])
const actionColumn = () => {
columns.push(
{
dataField: 'actions',
text: actionConfig.text,
sort: false,
formatter: (row, cell) => {
return (
<UncontrolledDropdown>
<DropdownToggle tag="button" className="btn btn-link arrow-none card-drop p-0">
<i className="mdi mdi-dots-horizontal" style={{ fontSize: 25, color: "#6c757d" }}></i>
</DropdownToggle>
<DropdownMenu right>
{actionConfig.itens.map(e => {
return (<DropdownItem onClick={() => e.myEvent(cell)}><i className={e.icon}></i>{e.label}</DropdownItem>)
})}
</DropdownMenu>
</UncontrolledDropdown>
)
}
})
}
return (
<>
<Card>
<CardBody>
<ToolkitProvider
bootstrap4
keyField="id"
data={records}
columns={columns}
search={hasSearch}
exportCSV={{ onlyExportFiltered: true, exportAll: false }}
columnToggle={hasToogle}>
{props => (
<React.Fragment>
<Row>
{hasToogle ?
<div>
<CustomToggleList {...props.columnToggleProps} />
</div> : null}
{hasSearch ?
<Col>
<SearchBar {...props.searchProps} />
</Col> : null}
{exportCsv ?
<Col className="text-right">
<ExportCSVButton {...props.csvProps} className="btn btn-primary">
Export CSV
</ExportCSVButton>
</Col> : null}
</Row>
<BootstrapTable
condensed={isCondensed}
{...props.baseProps}
bordered={bordered}
defaultSorted={sorted}
pagination={hasPagination ? paginationFactory(paginationOptions) : null}
wrapperClasses="table-responsive"
/>
</React.Fragment>
)}
</ToolkitProvider>
</CardBody>
</Card>
</>
);
}
export default RelevoTable;
在这里,我将其称为组件:
render() {
var actionConfig = {
text: "My Actions",
itens: [{icon: "mdi mdi-grill-outline", label: "teste", myEvent: this.alertAction},{icon: "mdi mdi-grill-outline", label: "teste", myEvent: this.alertAction}]
}
return (
<>
<RelevoTable
hasPagination={true}
records={records}
columns={columns}
sorted={[
{
dataField: 'id',
order: 'asc',
}
]}
hasToogle={true}
hasSearch={true}
exportCsv={true}
bordered={true}
isCondensed={false}
actionConfig={actionConfig}
/>
</>
);
}