javascript - 如何在反应中将两个函数传递给onClick事件
问题描述
我想将两个函数传递给onClick
event which ishandleSubmit
和from handleDelete
theHomePage.js
HomeItem.js
这是我的错误:
No duplicate props allowed react/jsx-no-duplicate-props.
这是我的 HomePage.js:
const HomePage = props => {
const tvshow = props.item;
let res;
if (tvshow.length > 0) {
res = tvshow.map(res=> (
<Content item={res} onClick={props.onClick}/>
));
}
return (
<div>
<Container>
<Row>{res}</Row>
</Container>
</div>
);
};
export default HomePage;
这是我的 HomeItem.js:
const HomeItem = props => {
function handleSubmit() {
props.onClick({
name: props.item.name,
id: props.item.id
});
}
function handleName() {
props.onClick({
name: props.item.name
});
}
<Button onClick={handleSubmit}></Button>
<Button onClick={handleName}></Button>
这是我的 App.js:
handleSubmit(newFavorite) {}
handleName(newFavorite) {}
render() {
<Route
exact
path="/"
render={() => (
<HomePage
item={this.state.SaveFavorite}
onClick={this.handleSubmit}
onClick={this.handleName}
/>
)}
/>
}
所以我的问题是如何将 2 onClick 函数放到 Hompage.js
解决方案
这个怎么样:
<HomePage
item={this.state.SaveFavorite}
onClick={(favorite)=>{
this.handleSubmit(favorite);
this.handleName(favorite);
}
}
/>
这假设您的目标是一次调用两个函数。如果它们应该在不同的情况下被调用,请给一个函数一个不同的名称,例如onSubmit
or onNameChange
。
推荐阅读
- php - 为什么有人不会省略结束 php 标签?
- python - Django 将模型字段作为参数传递给存储在另一个模型中的 URL
- node.js - 如何在 Sequelize 中使用 Postgres 的 BIGSERIAL?
- mongodb - mongo 在顶部插入数组而不重复
- c# - componentOne windows flexgrid 自动化使用 CodedUI C#
- vba - 有没有办法在 VBA Excel 中使用多个工作表查找和替换?
- r - 使用ggmap创建地图和持久错误R
- c# - 如何调用扩展方法从数组中删除项目
- powershell - 带参数的 PowerShell 调用运算符
- azure-devops - How can I automatically link variable groups and agent pool when import release definitions to Azure DevOps?