"arrow function expected no return value" useEffect 中有清理功能
"arrow function expected no return value" with clean-up function in useEffect
这是我的 useEffect
,带有简单的清理功能 () => { inbox?.destroy(); }
,但是当我在那里使用清理功能时它会发出警告。为什么会这样,清理函数不是合法的语法吗?如何解决(当然不删除清理功能)?
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]);
可能的解决方法: 将 return;
改为 return undefined;
解释:
This rule requires return
statements to either always or never specify values
在第 4 行,if (!currentUser || !talkjsContainerRef.current) return;
,我的 return
语句 没有指定值 ,这与我的 矛盾清理函数.
这是我的 useEffect
,带有简单的清理功能 () => { inbox?.destroy(); }
,但是当我在那里使用清理功能时它会发出警告。为什么会这样,清理函数不是合法的语法吗?如何解决(当然不删除清理功能)?
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]);
可能的解决方法: 将 return;
改为 return undefined;
解释:
This rule requires
return
statements to either always or never specify values
在第 4 行,if (!currentUser || !talkjsContainerRef.current) return;
,我的 return
语句 没有指定值 ,这与我的 矛盾清理函数.