Temporarily resolved keydown repeat event bug by circumventing sendData empty string alert.

This commit is contained in:
Zhongheng Liu 2024-01-14 14:50:37 +02:00
commit 4d1d0d7d98
No known key found for this signature in database

View file

@ -36,7 +36,6 @@ const Chat = ({ user }: { user: string }): React.ReactElement => {
/>,
]);
});
console.log(messages);
});
stompClientRef.current.publish({
body: JSON.stringify({
@ -62,14 +61,11 @@ const Chat = ({ user }: { user: string }): React.ReactElement => {
// Button press event handler.
const sendData = () => {
console.log("WebSockets handler invoked.");
// There must be a react-native and non-document-getElementById way to do this
// TODO Explore
const entryElement: HTMLInputElement = document.getElementById(
"data-entry"
) as HTMLInputElement;
if (!entryElement.value) {
alert("Message cannot be empty!");
if (entryElement.value === "") {
return;
}
const messageData: Message = {
@ -98,10 +94,10 @@ const Chat = ({ user }: { user: string }): React.ReactElement => {
return () => {
stompClientRef.current.deactivate();
};
}, [stompClientRef]);
}, []);
// https://www.w3schools.com/jsref/obj_keyboardevent.asp
document.addEventListener("keypress", (ev: KeyboardEvent) => {
if (ev.key == "Enter") {
document.addEventListener("keydown", (ev: KeyboardEvent) => {
if (ev.key === "Enter") {
sendData();
}
});