When we were developing SUSI AI web chat some components became huge. So the team wanted to break some components into parts. Since the Login dialog-box is used in several places we decided to make a separate component for Login Dialog-box. In this post I am discussing how we implemented the feature as a separate component and how we have changed the state of the parent component of the child component.
Login Dialog-box contains all the things of the login dialog-box component.
Child-component (Login Dialog-box component) is here:
This method executes the ‘switchDialog’ function of the parent component.
In this part we validate property types that has passed from the parent component.
In render() method I have returned the element.
To open and close dialog we have to communicate with parent component. We can send an instruction as an attribute of the element and we can refer it inside the element as “props”. This is how I have sent an instruction to the child element.
‘handleOpen’ function opens the dialog when user hit on the login button.
‘switchDialog’ function is using for change the state of parent component from child Component (Login Dialog-box component).
To open and close the dialog-box we have to send the state of the parent component to child component. To close the dialog-box we have to update the parent component’s state from child component.
To change the parent component’s state we have used this in element.
To send the state to the child component we used this.
To send other properties to the element we used this.
After closing the dialog-box it calls this method and it updates the state of the parent component.
This is how we can communicate between child and parent components using react.
Component Communication: http://andrewhfarmer.com/component-communication/
Material UI Dialogs: http://www.material-ui.com/#/components/dialog