Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  125] [ 3]  / answers: 1 / hits: 6018  / 3 Years ago, sun, may 23, 2021, 12:00:00

Here is my useEffect with a simple clean-up function () => { inbox?.destroy(); }, but it raises a warning when I let the clean-up function there. Why is that, isn't the clean-up function a legit syntax? How to fix it (of course without removing the clean-up function)?


enter


  useEffect(() => {
const { currentUser } = initialState!;
let inbox: Talk.Inbox;
if (!currentUser || !talkjsContainerRef.current) return;

Talk.ready.then(async () => {
const me = employeeToUser(currentUser);
window.talkSession = new Talk.Session({ appId, me });
if (id === undefined) {
// me without other => most recent message first
inbox = window.talkSession.createInbox();
} else {
// me with an other => select other
const other = employeeToUser(await readEmployee(Number(id)));
const conversation = window.talkSession.getOrCreateConversation(Talk.oneOnOneId(me, other));
conversation.setParticipant(me);
conversation.setParticipant(other);
inbox = window.talkSession.createInbox({ selected: conversation });
}
inbox.mount(talkjsContainerRef.current);
});

return () => {
inbox?.destroy();
};
}, [id, initialState]);

More From » reactjs

 Answers
6

A possible fix: turn return; to return undefined;


enter


Explanation:


I had violated the consistent-return rule



This rule requires return statements to either always or never specify values



At line 4, if (!currentUser || !talkjsContainerRef.current) return;, my return statement didn't specify a value, which is contradictory to my "clean-up function".


[#1325] Monday, May 17, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
Tue, Jun 21, 22, 00:00, 2 Years ago
Tue, Feb 8, 22, 00:00, 2 Years ago
Thu, Mar 4, 21, 00:00, 3 Years ago
Sat, Jan 16, 21, 00:00, 3 Years ago
;