gpt4 book ai didi

javascript - 将 onClick 添加到 React 中的提交按钮

转载 作者:行者123 更新时间:2023-11-30 20:04:11 25 4
gpt4 key购买 nike

我在 react 中有这个按钮

  {editing && (
<Button extraClasses="m1" onClick={this.handleEditing} type="submit">
Save
</Button>
)}

但是提交不起作用,如果我删除 onClick,提交就会起作用。我怎样才能让 onClick 和提交都起作用?

这是 onSubmit 事件:

  handleSubmit(e) {
e.preventDefault();
const params = this.state.params || this.props.selected.params;
const { exportTypes } = this.props;
const {
startDate: startDateMoment,
endDate: endDateMoment,
companyId,
exportTypeId,
id,
} = this.state;
const type = exportTypes.find(o => o.id === Number(exportTypeId));
let enrichedParams = [];

if (type.params.length > 0) {
enrichedParams = params.reduce((acc, { paramName, paramValue }) => {
const { id: exportParameterId } = type.params.find(p => p.name === paramName);
return [...acc, { exportParameterId, paramName, paramValue }];
}, []);
}

const startDate = startDateMoment.format();
const endDate = endDateMoment.format();
const record = { companyId, exportTypeId, startDate, endDate, id, params: enrichedParams };
const filteredQuery = Object.keys(record).reduce(
(acc, k) => (record[k] ? { ...acc, [k]: record[k] } : acc),
{},
);
if (!Object.keys(filteredQuery).length) return;
this.props.updateExport(filteredQuery);
}

最佳答案

您可以从 Button 中删除 onClick 事件处理程序,并在 handleSubmit 方法中调用 handleEditing 方法相反。

示例

class App extends React.Component {
handleEditing = () => {
// ...
};

handleSubmit = (e) => {
// ...
this.handleEditing();
};

render() {
return (
<div>
{/* ... */}
{editing && (
<Button extraClasses="m1" type="submit">
Save
</Button>
)}
{/* ... */}
</div>
);
}
}

关于javascript - 将 onClick 添加到 React 中的提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53093151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com