I'm gettin undefined in hubConnection, and my program doesn't work, but after refreshing everything works correctly. I've tried to make hubConnection as simple property, but it didn't help me. One more thing that I've already tried is to create useRef hook and check if hubConnection if not undefined, if it not, then block the useEffect, but my messages after this action has stopped work.
Code of the program:
import React, { useCallback, useState, useEffect, useContext } from "react";
import { View, StyleSheet } from "react-native";
import { routes } from "../../../../Environment";
import { Icon } from 'react-native-elements'
import Font from "../../../data/fonts/Font";
import { GiftedChat, Bubble, Send, InputToolbar } from 'react-native-gifted-chat'
import { container } from 'tsyringe';
import ChatService from "../../../../api-service/chat-service/ChatService";
import { AuthContext } from "../../auth/AuthProvider";
import { HubConnection, HubConnectionBuilder } from "@microsoft/signalr";
const Chat = (props: any) => {
const chatService = container.resolve(ChatService);
const [messages, setMessages] = useState([]);
const [message, setMessage] = useState("");
const [hubConnection, setHubConnection] = useState<HubConnection>();
const { user } = useContext(AuthContext);
const getRandomInt = (max: number) => {
return Math.floor(Math.random() * Math.floor(max));
}
useEffect(() => {
setMessages([
{
_id: 1,
text: 'Hello dude',
createdAt: new Date(),
user: {
_id: 2,
name: 'React Native',
avatar: 'https://placeimg.com/140/140/any',
},
},
{
_id: 2,
text: 'Hello Tom',
createdAt: new Date(),
user: {
_id: user?.id.toString()!,
name: 'React Native',
avatar: 'https://placeimg.com/140/140/any',
},
},
{
_id: 3,
text: 'How are you?',
createdAt: new Date(),
user: {
_id: 2,
name: 'React Native',
avatar: 'https://placeimg.com/140/140/any',
},
},
])
const hubConnectionFunc = new HubConnectionBuilder()
.withUrl(routes.chatUrl)
.build();
hubConnectionFunc?.start().then(() => "Connection started!");
hubConnectionFunc.on("RecieveMessage", message => {
console.log(messages[0].text);
setMessages(previousMessages => GiftedChat.append(previousMessages, {
_id: getRandomInt(10000),
text: message,
createdAt: new Date(),
user: {
_id: user?.id.toString()!,
name: 'React Native',
avatar: 'https://placeimg.com/140/140/any',
},
}))
})
setHubConnection(hubConnectionFunc);
console.log(hubConnection);
}, [])
const onSend = useCallback((messages = []) => {
hubConnection?.invoke("SendMessage", messages[0].text)
.catch((err: any) => console.log(err));
setMessage("");
}, [])
const renderBubble = (props: any) => {
return (
<Bubble
{...props}
wrapperStyle={{
left: {
backgroundColor: "#F1F1F4",
},
right: {
backgroundColor: "#EB7A89"
}
}}
textStyle={{
left: {
color: "#000000"
},
right: {
color: "#FFFFFF"
}
}}
/>
);
}
const renderSend = (props: any) => {
return (
<Send {...props}>
<View style={styles.button}>
<Icon
name='paper-plane'
type='font-awesome'
color='white'
/>
</View>
</Send>
)
}
const renderInputToolbar = (props: any) => {
return (
<InputToolbar {...props} primaryStyle={{
borderWidth: 2,
justifyContent: "center",
alignItems: "center",
height: 44
}}
/>
)
}
return (
<View style={styles.chatWrapper}>
<GiftedChat
placeholder="Aa"
renderTime={() => <View></View>}
maxInputLength={500}
messages={messages}
onInputTextChanged={setMessage}
text={message}
onSend={onSend}
scrollToBottom
alwaysShowSend
user={{
_id: user?.id.toString()!,
name: user?.name!,
}}
renderBubble={renderBubble}
renderSend={renderSend}
renderInputToolbar={renderInputToolbar}
/>
</View >
)
}