I'm still not sure how to get this to work. I wanted to write a small chat app in expo. Right now I'm using a Button that, when clicked, should append a message to a gifted chat. I have two components that are not connected. One is called button.js and one chat.js. They are both displayed in the App.js. I tried writing a helper function in chat.js
updateChatView(){
//Debug only
// console.log("TEST");
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, {
_id: "MSG ID",
text: "TEXT YOU WANT TO SHOW",
createdAt: new Date(),
user: {
_id: "USER_ID",
name: "USER NAME",
avatar:"USER AVATAR URL",
},
}),
}));
}
I can see the console output TEST, but the state can't be set, because it seems like the Component (chat.js) is not mounted. I use this code to call the function.
import ChatView from './chatView';
const cv = new ChatView();
//Render function
<View style={styles.container}>
<Button title="Button" onPress={(e) => cv.updateChatView()} />
</View>
I would think that the chat.js is mounted, as it's displayed? I have no real idea where to start. I tought about replacing state with redux, but I'm not sure if this would work.