I have a parent component PicturesFetchthat displays another (child) component Picture, which displays a div with an image inside. Now for rendering Picture, it PicturesFetchpasses through the object (pics), and using the .map () function creates several divs with images inside. I want to be able to change the state of each item created separately. Here is what I have tried so far, verified bindings were checked one by one, and not all together:
Picturefetch.js
class PicturesFetch extends React.Component {
constructor(props) {
super(props);
this.state = {
isSelected: true,
pictureClassName: "picture-div green",
pics : []
};
this.handlePictureClick = this.handlePictureClick.bind(this);
}
handlePictureClick (isSelected){
if (!isSelected) {
this.setState({
isSelected: true,
pictureClassName: "picture-div green"
})
} else {
this.setState({
isSelected: false,
pictureClassName: "picture-div none"
})
}
}
render() {
var changeClass = this.state.pictureClassName;
var handlePictureClick = this.handlePictureClick.bind(this);
var pics = this.state.pics.map(function(pic, i){
if () {
return (
<div className="pic-div" key={i} >
<Picture
isSelected={isSelected}
pictureClassName={changeClass}
onClick={handlePictureClick.bind(this, isSelected)} /*#3 tested bind*/
/>
</div>
});
}
return (
<div className="Options-container">
<div className="container">{pics}</div>
</div>
);
}}
Picture.js
class Picture extends React.Component {
constructor(props) {
super(props);
this.state = {
isSelected: true,
pictureClassName: "picture-div green"
};
}
render() {
var pictureClassName = this.props.pictureClassName;
return (
<div onClick={this.props.onClick} className={pictureClassName}>
<img src={this.props.src} alt={this.props.name} />
<h5>{this.props.name}</h5>
</div>
)
}}
. , №1 №2, , # 3 "setState" undefined.
onClick, ? , ? .