I know this question has been asked before but the answers always seemed to hint at a problem unrelated to this scenario and I just don't see it.
I have a parent component which passes a property to a child component's state. Depending on the value of this passed property the child initially shows its view or not. Here is the way I thought React works:
When a parent's (or a component in general) state changes, it will be re-rendered. When it gets re-rendered, all of its children receive their props again (i.e. the constructor of the child is also invoked?). But that is apparently not how it works in this example.
Parent component:
class FileListTable extends React.Component
{
constructor(props)
{
super(props);
this.state = {
showModal: false
};
this.showSelectFolderModal = this.showSelectFolderModal.bind(this);
}
showSelectFolderModal()
{
this.setState({ showModal: true });
}
render()
{
console.log("Rendering table with showModal = " + this.state.showModal);
return (
<div>
<table id="files-table" class="table">
<tr>
<td>
...
<span class="float-right">
<FileActionMenu
showSelectFolderModal={ this.showSelectFolderModal } />
</span>
</td>
</tr>
</table>
<SelectFolderModal
show={ this.state.showModal }/>
</div>
);
}
}
FileActionMenu
component child:
class FileActionMenu extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
return (
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
...
</button>
<div class="dropdown-menu dropdown-menu-right">
<button class="dropdown-item" type="button" onClick={ () => this.props.showSelectFolderModal() }>...</button>
</div>
</div>
);
}
}
SelectFolderModal
component child:
class SelectFolderModal extends React.Component
{
constructor(props)
{
super(props);
console.log("Received props from table");
console.log(this.props);
this.state = { show: this.props.show };
}
closeModal()
{
this.setState({ show: false });
// do something post close
}
render()
{
console.log("Rendering modal with show status: " + this.state.show);
return (
<div id="select-folder-modal" class="modal { this.state.show ? 'show' }" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" onClick={ this.closeModal }>...</button>
</div>
</div>
</div>
</div>
);
}
}
My initial console output is this:
Rendering table with showModal = false
Received props from table
{show: false}
Rendering modal with show status: false
Okay, this makes sense. But when i click on the button in FileActionMenu
i get the following output:
Rendering table with showModal = true
Rendering modal with show status: false
So the state of the parent is not passed as props to the child when the parent is updating! But I thought this is how it works, can someone shed some light on this for me?