Reputation: 3307
I've got a component that trigger an modal window when users click on the delete record in the UI,but the problem is that, the lifecycle method componentDidUpdate
is only called once,th first time.
For exemple,is i click on the delete Button,the state is updated in the child component that holds the Modal component,the all the other actions after the first one dont trigger Modal show.
Here is my code:
import React, { Component } from 'react';
import { Button, Header, Icon, Modal } from 'semantic-ui-react';
class ConfirmationBox extends Component {
state = { isOpen: false };
componentDidMount() {
this.setState({ isOpen: this.props.isOpen });
}
componentDidUpdate(prevProps, nextProps) {
if (prevProps.isOpen !== this.props.isOpen) {
this.setState({ isOpen: this.props.isOpen });
}
}
closeConfirmation = () => {
this.setState({ isOpen: !this.state.isOpen });
};
onConfirmDelection = () => {
this.closeConfirmation();
};
onCancelDelection = () => {
this.closeConfirmation();
};
render() {
return (
<Modal open={this.state.isOpen} basic size='small'>
<Header icon='delete' content='Confirmer suppression' />
<Modal.Content>
<p>Souhaitez-vous vraiment supprimer {this.props.names}?</p>
</Modal.Content>
<Modal.Actions>
<Button onClick={this.onCancelDelection} basic color='red' inverted>
<Icon name='remove' /> No
</Button>
<Button onClick={this.onConfirmDelection} color='green' inverted>
<Icon name='checkmark' /> Yes
</Button>
</Modal.Actions>
</Modal>
);
}
}
export default ConfirmationBox;
Upvotes: 1
Views: 855
Reputation: 834
You don't have to store isOpen in state and in props. Only single source of true is correct way. For example the next way is stored isOpen in props
import React, { Component } from 'react';
import { Button, Header, Icon, Modal } from 'semantic-ui-react';
class ConfirmationBox extends Component {
onConfirmDelection = (data) => {
const { closePopup, confirmDeletion } = this.props;
closePopup();
confirmDeletion(data);
}
render() {
const { isOpen, closePopup, names } = this.props;
return (
<Modal open={isOpen} basic size='small'>
<Header icon='delete' content='Confirmer suppression' />
<Modal.Content>
<p>Souhaitez-vous vraiment supprimer {names}?</p>
</Modal.Content>
<Modal.Actions>
<Button onClick={closePopup} basic color='red' inverted>
<Icon name='remove' /> No
</Button>
<Button onClick={this.onConfirmDelection} color='green' inverted>
<Icon name='checkmark' /> Yes
</Button>
</Modal.Actions>
</Modal>
);
}
}
export default ConfirmationBox;
Upvotes: 2
Reputation: 583
in fact, you don't update the isOpen property in the parent component when trigger closeConfirmation. It just updates the isOpen in the ConfirmationBox state. That's why prevProps.isOpen === this.props.isOpen, they are both TRUE. You should move closeConfirmation to the parent component and use isOpen from props.
Upvotes: 0
Reputation: 4272
On your componentDidUpdate
method why you have a nextProps
param if you are using this.props
Upvotes: 0