Anonymous
Приложение вылетает при отправке сообщения в response-native-gifted-chat
Сообщение
Anonymous » 23 окт 2024, 00:41
У меня происходит сбой в приложении Expo, когда я отправляю сообщение с помощью компонента Gifted Chat. Приложение отлично работает в симуляторе Android, но вылетает, когда я собираю APK и запускаю его на физическом устройстве.
Шаги для воспроизведения:
Создайте приложение Expo в виде предварительной сборки (APK).
Установите APK на физическое устройство Android.
Открыть приложение и перейдите к экрану чата.
Введите сообщение и нажмите кнопку «Отправить».
Приложение сразу же вылетает.
Код: Выделить всё
import React, { useState, useCallback, useEffect } from "react";
import { Bubble, GiftedChat, Send } from "react-native-gifted-chat";
import { SafeAreaView, View, Text } from "react-native";
import { Colors } from "../../constants/Colors";
import { Ionicons } from "@expo/vector-icons";
import { useSafeAreaInsets } from "react-native-safe-area-context";
const ChatScreen = () => {
const insets = useSafeAreaInsets();
const [messages, setMessages] = useState([]);
const [isTyping, setIsTyping] = useState(false);
const [text, setText] = useState("");
useEffect(() => {
test();
}, []);
const test = async (userMessage) => {
setIsTyping(true);
try {
const newMessage = {
_id: Date.now(),
text: "api response text",
createdAt: new Date(),
user: { _id: 2, name: "AI Assistant" },
isStarred: false,
};
setMessages((prev) => GiftedChat.append(prev, [newMessage]));
} catch (error) {
console.error("API Error:", error);
} finally {
setIsTyping(false);
}
};
const onSend = useCallback(async (messages = []) => {
setMessages((prev) => GiftedChat.append(prev, messages));
await test(messages[0].text);
}, []);
const onLongPress = (context, message) => {
setMessages((prev) =>
prev.map((msg) =>
msg._id === message._id ? { ...msg, isStarred: !msg.isStarred } : msg
)
);
};
const renderSend = (props) => (
);
const renderBubble = (props) => (
(
{new Date(props.currentMessage.createdAt).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})}
{props.currentMessage.isStarred && (
)}
)}
/>
);
return (
);
};
const styles = {
timeContainer: { flexDirection: "row", alignItems: "center" },
timeText: { fontSize: 12, color: "#666", marginRight: 5 },
sendButton: {
backgroundColor: Colors.primary,
padding: 10,
borderRadius: 10,
justifyContent: "center",
alignItems: "center",
},
inputMsg: {
backgroundColor: Colors.card,
borderWidth: 1,
borderRadius: 10,
borderColor: "#666",
paddingHorizontal: 10,
fontSize: 16,
marginVertical: 4,
paddingTop: 8,
},
};
export default ChatScreen;
Версия Nodejs: v20.12.1
Версия React: 18.2.0
< li>Версия React Native: 0.74.5
Версия React-native-gifted-chat: 2.6.4
Платформы (iOS, Android или оба?): Android
Версия TypeScript: 5.3.3
Подробнее здесь:
https://stackoverflow.com/questions/791 ... ifted-chat
1729633306
Anonymous
У меня происходит сбой в приложении Expo, когда я отправляю сообщение с помощью компонента Gifted Chat. Приложение отлично работает в симуляторе Android, но вылетает, когда я собираю APK и запускаю его на физическом устройстве. Шаги для воспроизведения: [list][*]Создайте приложение Expo в виде предварительной сборки (APK). [*]Установите APK на физическое устройство Android. [*]Открыть приложение и перейдите к экрану чата. [*]Введите сообщение и нажмите кнопку «Отправить». [*]Приложение сразу же вылетает.[/list] [code]import React, { useState, useCallback, useEffect } from "react"; import { Bubble, GiftedChat, Send } from "react-native-gifted-chat"; import { SafeAreaView, View, Text } from "react-native"; import { Colors } from "../../constants/Colors"; import { Ionicons } from "@expo/vector-icons"; import { useSafeAreaInsets } from "react-native-safe-area-context"; const ChatScreen = () => { const insets = useSafeAreaInsets(); const [messages, setMessages] = useState([]); const [isTyping, setIsTyping] = useState(false); const [text, setText] = useState(""); useEffect(() => { test(); }, []); const test = async (userMessage) => { setIsTyping(true); try { const newMessage = { _id: Date.now(), text: "api response text", createdAt: new Date(), user: { _id: 2, name: "AI Assistant" }, isStarred: false, }; setMessages((prev) => GiftedChat.append(prev, [newMessage])); } catch (error) { console.error("API Error:", error); } finally { setIsTyping(false); } }; const onSend = useCallback(async (messages = []) => { setMessages((prev) => GiftedChat.append(prev, messages)); await test(messages[0].text); }, []); const onLongPress = (context, message) => { setMessages((prev) => prev.map((msg) => msg._id === message._id ? { ...msg, isStarred: !msg.isStarred } : msg ) ); }; const renderSend = (props) => ( ); const renderBubble = (props) => ( ( {new Date(props.currentMessage.createdAt).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", })} {props.currentMessage.isStarred && ( )} )} /> ); return ( ); }; const styles = { timeContainer: { flexDirection: "row", alignItems: "center" }, timeText: { fontSize: 12, color: "#666", marginRight: 5 }, sendButton: { backgroundColor: Colors.primary, padding: 10, borderRadius: 10, justifyContent: "center", alignItems: "center", }, inputMsg: { backgroundColor: Colors.card, borderWidth: 1, borderRadius: 10, borderColor: "#666", paddingHorizontal: 10, fontSize: 16, marginVertical: 4, paddingTop: 8, }, }; export default ChatScreen; [/code] [list] [*]Версия Nodejs: v20.12.1 [*]Версия React: 18.2.0 < li>Версия React Native: 0.74.5 [*]Версия React-native-gifted-chat: 2.6.4 [*]Платформы (iOS, Android или оба?): Android [*]Версия TypeScript: 5.3.3 [/list] Подробнее здесь: [url]https://stackoverflow.com/questions/79115824/app-crashes-when-sending-message-in-react-native-gifted-chat[/url]