[英]ReactJS onClick event to another component
如何在锚点上使用onClick
在另一个组件内设置状态?
带有onClick
Card.js
import React from 'react'
import PropertyLightbox from '../global/PropertyLightbox'
const Card = (props) => {
const propertyImages = props.gallery
return (
<Property id={props.slug}>
<a onClick={this.openLightbox} href="javascript:;">Click to open lightbox</a>
<PropertyLightbox lbImages={propertyImages}/>
</Property>
)
}
export default Card
与PropertyLightbox.js setState
openLightbox
class propertyLightbox extends React.Component {
constructor() {
super();
this.state = { currentImage: 0 };
this.openLightbox = this.openLightbox.bind(this);
}
openLightbox(event, obj) {
this.setState({
currentImage: obj.index,
lightboxIsOpen: true,
});
}
render() {
return (
<Lightbox images={this.props.propertyImages}
isOpen={this.state.lightboxIsOpen}
</Lightbox>
)
}
}
propertyLightbox.propTypes = {
propertyImages: PropTypes.func,
}
export default propertyLightbox
你可以
hasOpenLightBox
hasOpenLightBox
到true
的this.openLightbox
<PropertyLightbox isOpen={this.state.hasOpenLightBox}
isOpen
PropertyLightbox中执行所需的任何操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.