I have an array of items which individually has a button, I want to disable the button of an item when I click on the Item. I have tried creating an array of disabled items in the state and setState onClick of the Button but I keep getting errors. Here is my Component code below.
class MovieSummary extends Component {
constructor(props) {
super(props);
this.state = {
disabled: [],
};
}
addingItem = (item) => {
this.props.addItem(item);
};
render() {
const { Title, imdbID } = this.props.movie;
return (
<div>
{" "}
<h5>
{this.props.movie.Title}{" "}
<button
key={imdbID}
disabled={this.state.disabled.indexOf(imdbID) !== -1}
onClick={
(() =>
this.addingItem({
title: Title,
id: imdbID,
}),
this.setState({
disabled: [...this.state.disabled, imdbID],
}))
}
>
Nominate
</button>
</h5>
</div>
);
}
}
export default MovieSummary;
this.props.addItemto also set a disabled flag on your movie object?onClick = { () => this.addingItem({...}); this.setState({ ... }) }You know what to fill in place of '...'