Onclick在此组件中不起作用。这是我第一次遇到此错误,我尝试遍历代码,将所有函数都绑定到this
,但是onClick
方法根本不起作用。我在div
内添加到Col
的所有className也没有被应用。
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>
);
}
}
答案 0 :(得分:-2)
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
时,您就会在其中填充一个带有每次调用时都会生成的键prop的组件数组,当react对帐时在这种情况下,子组件将不会重复使用。