useEffect 中带有清理函数的“箭头函数预期无返回值”

Huỳ*_*yễn 16 javascript reactjs eslint use-effect

这是我的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]);
Run Code Online (Sandbox Code Playgroud)

Huỳ*_*yễn 46

return;可能的修复:转到return undefined;

在此输入图像描述

解释:

我违反了consistent-return规则

此规则要求return语句始终或从不指定值

在第4行,if (!currentUser || !talkjsContainerRef.current) return;我的return语句没有指定值,这与我的“清理功能”相矛盾