I have a list which has this Modal component. If a user clicks a certain row in the list, the props within that row are passed down to the Modal Component
.
When the props are passed to the Modal, I want to call componentWillReceiveProps
to set the props passed as part of the Modal's state. I do this because props like title
, status
and cost
are eventually going to be controlled inputs that are updated via the Modal's state.
My only problem is componentWillReceiveProps
is only called once and the props are not passed down to the Modal's state. Is there a way to pass these props to the child component as the state?
I'm using the Modal from React-Bootstrap
Parent Component:
<AdditionalPaymentsModal
status={'New'}
id= {null}
title={'testong'}
cost={''} />
Child Component:
class AdditionalPaymentsModal extends Component {
constructor(props){
super(props);
this.state = {
showModal: this.props.showModal,
};
this.close = this.close.bind(this);
this.open = this.open.bind(this);
}
close() {
this.setState({ showModal: false });
}
open() {
this.setState({ showModal: true });
}
componentWillReceiveProps(nextProps){
console.log("SETTING THE STATE", this.state);
this.setState({
id: nextProps.id,
title:nextProps.title,
status: nextProps.status,
cost:nextProps.date
})
}
render(){
const {id, title, status, cost} = this.props;
return (
<div>
<Button
onClick={this.open}>
Open
</Button>
<Modal show={this.state.showModal} onHide={this.close}>
<Modal.Header closeButton>
<Modal.Title>New Edit Cost</Modal.Title>
</Modal.Header>
<Modal.Body>
{this.state.title}
{this.state.id}
{this.state.cost}
{this.state.status}
</Modal.Body>
<Modal.Footer>
<Button onClick={this.close}>Cancel</Button>
</Modal.Footer>
</Modal>
</div>
);
}
}
this.setState({ id: props.id, title: props.title, status: props.status, cost: props.date })
in yourconstructor
override, instead ofcomponentWillReceiveProps
– bryceprops
are being passed through correctly? what does aconsole.log(props)
belowsuper(props)
return? – bryce