javascript - React NextJS 的映射对象 JSX 内的 onClick 不起作用

标签 javascript reactjs next.js

我有一个组件:

import React, { Component } from 'react';
import ImageItem from '../components/ImageItem';

class ImageList extends Component {

    handleClick() {
        console.log('Testing testing...'); // ---> This is not working.
    }
    render() {
        const images = this.props.images.map(image => {
            return (
                <ImageItem
                    onClick={this.handleClick}
                    key={image.id}
                    image={image.src}
                    title={image.title}
                />
            );
        });

        return (
            <div className="image-list" ref={el => (this.el = el)}>
                {images}
            </div>
        );
    }
}

export default ImageList;

但是,当我的 onClick 位于映射函数内时,它不会在控制台中记录任何内容。

这是我的 ImageItem 组件:

import React, { Component } from 'react';

class ImageItem extends Component {
    render() {
        return (
            <a href="#">
                <img
                    className="portfolio-image"
                    src={this.props.image}
                    alt={this.props.title}
                />
            </a>
        );
    }
}

export default ImageItem;

我在这里做错了什么?

最佳答案

您没有将点击处理程序分配给您的组件,它应该如下所示:

class ImageItem extends Component {
    render() {
        return (
            <a href="#" onClick={this.props.onClick}>
                <img
                    className="portfolio-image"
                    src={this.props.image}
                    alt={this.props.title}
                />
            </a>
        );
    }
}

export default ImageItem;

关于javascript - React NextJS 的映射对象 JSX 内的 onClick 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57915424/

相关文章:

javascript - React 检索(某些)DOM 值的方法

node.js - 快速服务器重定向问题

javascript - 单击按钮jquery mobile时蓝色突出显示

forms - 如何在semantic-ui-react中禁用表单自动完成?

javascript - 为什么导入类会使此代码失败?

javascript - 为什么 styled-components 将我的属性添加到 DOM 中?

reactjs - Nextjs 公共(public)文件夹

javascript - 通用类型的 typescript 长度

javascript - Twitter Bootstrap 模态 : How to remove Slide down effect

JavaScript 设置日期错误