![](/img/trans.png)
[英]React Native useEffect - Warning: An effect function must not return anything besides a function, which is used for clean-up
[英]"arrow function expected no return value" with clean-up function in useEffect
这是我的useEffect
与简单的清理 function () => { inbox?.destroy(); }
() => { inbox?.destroy(); }
,但是当我在那里清理 function 时会发出警告。 为什么,清理 function 不是合法的语法? 如何修复它(当然不删除清理 function )?
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;
此规则要求
return
语句始终或从不指定值
在第 4 行, if (.currentUser ||;talkjsContainerRef.current) return;
,我的return
语句没有指定值,这与我的“清理功能”相矛盾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.