如何在锚点上使用onClick
来设置另一个组件中的setState?
Card.js 与onClick
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
答案 0 :(得分:0)
你可以
hasOpenLightBox
hasOpenLightBox
true
设为this.openLightbox
<PropertyLightbox isOpen={this.state.hasOpenLightBox}
isOpen
道具