I have two components, of the same level under the parent component and depending on what happens in one, I want it to trigger something to happen in the other component.
I have a callback function in my Parent and I passed it to one of the Child. In the child, when an Icon is clicked, I call the callback function through this.props.callback() which goes back and triggers the function in the parent. But I keep getting an error when I'm trying to update the state so that when the parent re-renders, the other component displays differently.
Parent:
getInitialState: function() {
return {
activeTab: undefined,
messages: [],
newMessage: undefined
}
},
callback: function() {
this.setState({
"activeTab": "newChatTab"
});
},
render: function() {
const { activeTab, recentMessages, messages, newMessage } = this.state;
return (
<Modal>
<div className="sms-chat-modal">
<ModalSC.Header>
<Icon name="commenting" color="white" />
SMS Chat
<Icon onClick={ this.handleClose } name="remove" color="white" float="right" />
</ModalSC.Header>
<ModalSC.Content>
<SMSChatLeftNav
recentMessages={ recentMessages }
callback={this.callback}
/>
<div className="tabs-wrapper">
{ activeTab == "newChatTab" ? <ChatTab messages={ messages } newMessage={ newMessage } /> : '' }
</div>
</ModalSC.Content>
</div>
</Modal>
);
}
Child:
render: function() {
return(
<FilterInput className= "search-field" placeholder="Search Recent Contacts">
<Icon onClick={this.props.callback()} name="edit" color="blue" />
</FilterInput>
)
}
Error:
Cannot update during an existing state transition