Fix [UI/UX] [Chat] [Front End] React Warning

- [+] refactor(chat.tsx): capture current input reference value for use in component unmount or dependencies change
This commit is contained in:
H0llyW00dzZ 2024-02-05 20:40:22 +07:00
parent 67ce78cac2
commit e05af75891
No known key found for this signature in database
GPG Key ID: 05C7FFFC0845C930

View File

@ -1044,10 +1044,14 @@ function _Chat() {
localStorage.removeItem(key);
}
// This function will be called when the component unmounts.
// Capture the current value of the input reference.
const currentInputRef = inputRef.current;
// This function will be called when the component unmounts or dependencies change.
return () => {
// Save the current input to local storage only if it is not a command.
const currentInputValue = inputRef.current?.value ?? "";
// Use the captured value from the input reference.
const currentInputValue = currentInputRef?.value ?? "";
if (!currentInputValue.startsWith(ChatCommandPrefix)) {
localStorage.setItem(key, currentInputValue);
}