reactjs - 绑定一个 props 函数
问题描述
我有两个 React 组件,Gallery 和 Image。Image 组件使用 Gallery 函数作为道具。
我可以在没有箭头功能的情况下在渲染中进行调用吗?
图片组件:
class Image extends React.Component {
constructor(props) {
super(props);
this.state = {
filter: 'none',
};
}
render() {
return (
<div>
<button className="image-icon" onClick={() => this.props.handleClone(this.props.i)} />
</div>
);
}
}
图库组件:
class Gallery extends React.Component {
constructor(props) {
super(props);
this.handleClone = this.handleClone.bind(this);
this.state = {
images: [],
};
}
handleClone(i) {
var newImages = this.state.images;
newImages = newImages.slice(0, i + 1).concat(newImages.slice(i));
this.setState({
images: newImages,
});
}
render() {
return (
<div>
<div className="gallery-root">
{this.state.images.map((dto, i) => {
return <Image key={'image-' + dto.id + '-' + i} i={i} handleClone={this.handleClone} />;
})}
</div>
</div>
);
}
}
谢谢。
解决方案
class Image extends React.Component {
constructor(props) {
super(props);
this.state = {
filter: 'none'
};
}
handleClick = () => {
this.props.handleClone(this.props.i);
};
render() {
return (
<div>
<button className="image-icon" onClick={this.handleClick} />
</div>
);
}
}
推荐阅读
- sql - 如何复制目标表中不存在的记录
- c++ - 将数组的内容打印到 .txt 文件中
- reactjs - 创建反应文件夹的npx安装错误
- laravel - 如何在 Laravel Eloquent 中防止重复?
- angular - 如何在 Angular 2+ 中使用 Vimeo API
- javascript - 量角器扫描源代码从数组中寻找任何元素 - 就像 Ruby 循环一样
- wso2 - 如何在不同的服务器上运行 wso2 分析和 wso2 api 管理器以向分析服务器显示 api 管理器的静态
- c# - 嵌套对象的 ASP.NET CORE Hot Chocolate 查询错误
- flutter - 我可以继续使用旧版本的颤振包吗
- c# - 如何实现访问者模式来收集物品的总重量和总金额?