I'm making a chat app and want to keep its parts separate.
For example, I have part of code where communication logic is described. So when somebody receives a message addMessage function can be used to pass it to react component:
import { addMessage } from './components/App.Chat.MessageField'
rtc.on('data', (data) => {
/.../
addMessage(message);
});
And addMessaget function is binded method of react component:
export var addMessage;
export default class MessageField extends Component {
constructor() {
/.../
addMessage = this.addMessage.bind(this);
}
render() {
/.../
}
addMessage(message) {
/.../
this.setState(/.../);
}
}
So far, everything worked well. But I doubt that's good solution. Can it cause some problems or is there any better ways to do it?
rtc.onlistener incomponentDidMountof the component, and remove the listener incomponentWillUnmount.MessageFieldclass will have aaddMessagemethod bound to itself, so it's not clear which component'saddMessageyou will invoke by doing it like that.