I have a chat component with a textarea that allows to send a message when the key "enter" is pressed. It works, but the textarea is not fully reset. Its value is empty, but there is a new empty line, which hampers the placeholder to be displayed.
Here is the code:
const [newMessage, setNewMessage] = useState("");
const onSendMessage = () => {
if (newMessage.length > 1) {
onSend(newMessage);
setIsWritting(false);
setNewMessage("");
}
return undefined;
};
const onPressEnter = ({ key }) => {
if (key === "Enter") {
onSendMessage();
return false;
}
};
useEffect(() => {
document.addEventListener("keydown", onPressEnter);
return () => document.removeEventListener("keydown", onPressEnter);
}, [onPressEnter]);