【问题标题】:onClick and className not working in reactjsonClick 和 className 在 reactjs 中不起作用
【发布时间】:2019-04-20 20:25:56
【问题描述】:

Onclick 在此组件中不起作用。这是我第一次遇到这个错误,我已经尝试过代码,我已经将所有函数绑定到this,但onClick 方法根本不起作用。我添加到 Col 内的 div 的任何类名也不会被应用。

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Row, Col } from 'antd';
import { safariTypes }  from '../../data/safariTypes';
import uuid from 'uuid/v1';
import '../../App.css';
const ThePeople = './Media/thePeople.jpeg';
const BoraBora = './Media/bora.jpg';
const Aerial = './Media/balloon.jpg';
const Family = './Media/family_small.jpg';
const Honeymoons = './Media/honeymoon_small.jpg';
const Wildlife = './Media/wildlife.jpg';


    class Highlights extends Component {
    constructor(props) {
        super(props);
        this.state = {

        }
        this.returnSafariItemsHighlights = this.returnSafariItemsHighlights.bind(this)
        this.show = this.show.bind(this)
    }
show = () => {


     console.log("hey");

 }

    returnSafariItemsHighlights(){

        var Feature = null

        var arr = []
        if(safariTypes){
            safariTypes.slice(0,6).map((item, key) => {
                if(item === 'Aerial e.g Hot air ballons'){
                    Feature = Aerial
                } else if(item === 'Wildlife') {
                    Feature = Wildlife
                } else if(item === 'Family and Holiday') {
                    Feature = Family
                } else if(item === 'Beach Holidays') {
                    Feature = BoraBora
                } else if(item === 'Culture and History') {
                    Feature = ThePeople
                } else if(item === 'Honeymoons'){
                    Feature = Honeymoons
                } 


                arr.push(

                <Col  xs={24} sm={12} md={12} lg={8} xl={8}  key={uuid()} onClick={this.show} >
                <div 
                className="bounceAnimation"  
                style={{ backgroundImage: 'url(' + Feature + ')', 
                backgroundSize: 'cover', 
                backgroundPosition: 'center center',
                backgroundRepeat: 'no-repeat',
                height: '40vh',
                paddingTop: '28vh',
                paddingLeft: '20px',
                paddingRight: '20px'
              }}>

                <div  style={{backgroundColor: "rgb(255, 255, 255, 0.4)", textAlign: "center", fontSize: "25px",padding: "5px", fontFamily: "Lobster", color: "green"}}>

                #{item}

                </div>
                </div>

                </Col>


                )
            })
        }
        return arr
    }
  render() {

    return (
      <div>{this.returnSafariItemsHighlights()}</div>


    );
  }
}

【问题讨论】:

  • 你的控制台说什么,给我们错误
  • 控制台上什么都没有。
  • 这个变量从哪里来的if(safariTypes){
  • 已导入,没有问题,数据存在。它只是一些单词的数组。
  • 你能展示你所有的代码吗? Col 大概来自图书馆,可能不接受 onClick 事件。

标签: javascript arrays reactjs


【解决方案1】:

看来 Col 好像不接受 onClick 属性,不如把onClick={this.show} 移到 Col 里面的 div 中如下

<Col
  xs={24} sm={12} md={12} lg={8} xl={8}  key={uuid()} >
  <div 
    onClick={this.show}
    className="bounceAnimation"  
    style={{ backgroundImage: 'url(' + Feature + ')', ...}}
  >
     ...
  </div>
</Col>

我想我发现了一个性能问题,每次渲染组件时它都会调用returnSafariItemsHighlights,在那里你会使用每次调用函数时生成的 key prop 填充组件数组,当 react 协调时它会发现在这种情况下,子组件将不会重复使用不同的键。

【讨论】:

  • 请在 cmets 中查看上面的示例 Col 确实接受 onClick
猜你喜欢
  • 2014-04-22
  • 2016-07-14
  • 2020-07-22
  • 2020-04-12
  • 1970-01-01
  • 2019-10-29
  • 2014-03-25
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多